How to invoke a JSON parsing utility for streaming API status checks

I’m having trouble with my code setup. I built a JSON API monitor that determines whether a streaming service is active or inactive. This functionality lives in a separate class called StreamStatusMonitor.java.

I want to execute this class periodically using a timer, but I’m struggling with the proper way to invoke it.

Here’s my current approach:

public void StatusMonitor() {
    streamTimer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            try {
                StreamStatusMonitor.initialize();  // This line fails
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
            System.out.println(StreamStatusMonitor.data);
        }
    }, 1000*10, 1000*10);
}

And here’s my StreamStatusMonitor class:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;

public class StreamStatusMonitor {
    public static JSONObject data;
    
    private static String readContent(Reader reader) throws IOException {
        StringBuilder content = new StringBuilder();
        int character;
        while ((character = reader.read()) != -1) {
            content.append((char) character);
        }
        return content.toString();
    }
    
    public static JSONObject fetchJsonFromUrl(String endpoint) throws IOException, JSONException {
        InputStream stream = new URL(endpoint).openStream();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
            String jsonContent = readContent(reader);
            JSONObject result = new JSONObject(jsonContent);
            return result;
        } finally {
            stream.close();
        }
    }
    
    public static void initialize(String[] parameters) throws IOException, JSONException {
        data = fetchJsonFromUrl("http://api.justin.tv/api/stream/list.json?channel=" + ConfigManager.targetChannel + "");
        System.out.println(data.toString());
        System.out.println(data.get("id"));
    }
}

Any suggestions on what I’m doing incorrectly would be appreciated.

You’re calling StreamStatusMonitor.initialize() without parameters, but your method expects a String array. That’s what’s breaking it.

Two ways to fix this:

  1. Pass an empty array: StreamStatusMonitor.initialize(new String[]{})
  2. Add an overloaded method without parameters (better option since you’re already using ConfigManager.targetChannel)

I’d go with option 2. Add this to your StreamStatusMonitor class:

public static void initialize() throws IOException, JSONException {
    data = fetchJsonFromUrl("http://api.justin.tv/api/stream/list.json?channel=" + ConfigManager.targetChannel);
    System.out.println(data.toString());
    System.out.println(data.get("id"));
}

This lets your timer code work as-is. The method signature mismatch is what’s causing the failure.