feat: refactor json fetcher
This commit is contained in:
parent
3c1ddbc291
commit
ad77a36da0
@ -10,7 +10,8 @@ import org.json.JSONArray;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class JSONFetcher {
|
public class JSONFetcher {
|
||||||
public static JSONObject fetch(URL url) throws IOException {
|
|
||||||
|
private static String fetchString(URL url) throws IOException{
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||||
conn.setRequestMethod("GET");
|
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 {
|
public static JSONArray fetchArray(URL url) throws IOException {
|
||||||
StringBuilder result = new StringBuilder();
|
String result = fetchString(url);
|
||||||
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 JSONArray(result);
|
||||||
return new JSONArray(result.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user