I’m building a Java application that needs to monitor whether a specific Twitch streamer is currently online. The goal is to return a boolean value indicating the streaming status. I’m using minimal json library for parsing the API response.
Here’s what I’ve implemented so far:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import com.eclipsesource.json.JsonObject;
public class StreamChecker {
public static void main(String[] args) throws Exception {
TwitchMonitor monitor = new TwitchMonitor();
monitor.setVerbose(true);
monitor.connect("irc.twitch.tv", 6667, "oauth_token");
}
public boolean checkChannelStatus() {
try {
URL apiUrl = new URL("https://api.twitch.tv/kraken/streams/somestreamer");
URLConnection connection = apiUrl.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = reader.readLine();
reader.close();
JsonObject jsonResponse = JsonObject.readFrom(response);
return jsonResponse.get("stream").isNull() ? false : true;
} catch (IOException ex) {
ex.printStackTrace();
}
return false;
}
}
I’m wondering about the return behavior. When the method returns false, should I expect to see “false” printed in the console output automatically, or do I need to handle the display myself?