feat: add JSONFetcher to fetch a JSON with URL

This commit is contained in:
Nemo D'ACREMONT 2024-11-14 10:42:50 +01:00
parent 8ce493501e
commit cb18096f2e
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276

View File

@ -0,0 +1,25 @@
package eirb.pg203.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class JSONFetcher {
public static JSONObject fetch(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);
}
}
return new JSONObject(result.toString());
}
}