feat: refactor json fetcher

This commit is contained in:
Martin Eyben 2024-11-19 10:44:07 +01:00
parent 3c1ddbc291
commit ad77a36da0

View File

@ -10,7 +10,8 @@ import org.json.JSONArray;
import org.json.JSONObject;
public class JSONFetcher {
public static JSONObject fetch(URL url) throws IOException {
private static String fetchString(URL url) throws IOException{
StringBuilder result = new StringBuilder();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
@ -21,22 +22,19 @@ public class JSONFetcher {
}
}
return result.toString();
return new JSONObject(result.toString());
}
public static JSONObject fetch(URL url) throws IOException {
String result = fetchString(url);
return new JSONObject(result);
}
public static JSONArray fetchArray(URL url) throws IOException {
StringBuilder result = new StringBuilder();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
for (String line; (line = reader.readLine()) != null; ) {
result.append(line);
}
}
String result = fetchString(url);
return new JSONArray(result.toString());
return new JSONArray(result);
}
}