I’m building a Discord chat bot and need to create a feature that detects whether a streamer is currently broadcasting or not.
I wrote some code but it keeps returning “null” even when I know the stream is active. Here’s what I have in my main application:
public void checkStreamStatus() {
statusTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
StreamStatusChecker();
System.out.println(StreamStatusChecker.apiResponse);
System.out.println(StreamStatusChecker.apiResponse);
}
}, 15000, 15000);
}
And here’s my stream status checker class:
package ChatBot;
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 StreamStatusChecker {
public static JSONObject apiResponse;
private static String readAllContent(Reader reader) throws IOException {
StringBuilder builder = new StringBuilder();
int character;
while ((character = reader.read()) != -1) {
builder.append((char) character);
}
return builder.toString();
}
public static JSONObject fetchJsonFromEndpoint(String endpoint) throws IOException, JSONException {
InputStream stream = new URL(endpoint).openStream();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, Charset.forName("UTF-8")));
String responseText = readAllContent(reader);
JSONObject response = new JSONObject(responseText);
return response;
} finally {
stream.close();
}
}
public static void main(String[] args) throws IOException, JSONException {
apiResponse = fetchJsonFromEndpoint("http://api.justin.tv/api/stream/list.json?channel="+BotMain.targetChannel+"");
System.out.println(apiResponse.toString());
System.out.println(apiResponse.get("stream_id"));
}
}
Can anyone help me figure out why this isn’t working properly?