feat: add JSONFetcher to fetch a JSON with URL
This commit is contained in:
parent
8ce493501e
commit
cb18096f2e
25
src/main/java/eirb/pg203/utils/JSONFetcher.java
Normal file
25
src/main/java/eirb/pg203/utils/JSONFetcher.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user