Ich versuche gerade einen Status von einem Minecraft Server abzufragen mithilfe einer API. Ich bekomme eine Antwort, jedoch weiß ich nicht, wie ich diese (das JSON File) auslesen kann. Soweit ist mein Code:
Weiß jemand, wie ich die Antwort auslesen kann und dann zum Beispiel einen Boolean haben kann mit online true/false?
LG
Java:
private static HttpURLConnection connection;
public static void main(String[] args) {
BufferedReader reader;
String line;
StringBuffer responseContent = new StringBuffer();
try {
URL url = new URL("https://api.mcsrvstat.us/2/hypixel.net");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
//System.out.println(status);
if(status > 299) {
reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
while((line = reader.readLine()) != null) {
responseContent.append(line);
}
reader.close();
}else {
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
responseContent.append(line);
}
reader.close();
}
System.out.println(responseContent.toString());
parse(responseContent.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
}
LG