I’m trying to build a Java application that can detect whether a specific Twitch streamer is currently online. The goal is to get a boolean response - true when they’re streaming and false when they’re not. I’m using minimal json library for parsing the API response.
Here’s what I’ve written 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 {
ChatBot myBot = new ChatBot();
myBot.setVerbose(true);
myBot.connect("irc.twitch.tv", 6667, "mytoken");
}
public boolean checkIfLive() {
try {
URL apiUrl = new URL("https://api.twitch.tv/kraken/streams/streamerName");
URLConnection connection = apiUrl.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = reader.readLine();
reader.close();
JsonObject responseJson = JsonObject.readFrom(response);
return (!responseJson.get("stream").isNull()) ? true : false;
} catch (IOException ex) {
ex.printStackTrace();
}
return false;
}
}
When the method returns false, should I expect to see “false” printed somewhere in the console output automatically?