feat: use JSONFetcher in WeatherAPI
This commit is contained in:
parent
cb18096f2e
commit
3a19820ffa
@ -3,10 +3,9 @@ package eirb.pg203;
|
|||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@ -20,7 +19,6 @@ public class WeatherAPI implements WeatherDataAPI{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject fetchWeather(int days, String city) throws IOException {
|
private JSONObject fetchWeather(int days, String city) throws IOException {
|
||||||
StringBuilder result = new StringBuilder();
|
|
||||||
URL url = URI.create(
|
URL url = URI.create(
|
||||||
String.format("https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d",
|
String.format("https://api.weatherapi.com/v1/forecast.json?key=%s&q=%s&days=%d",
|
||||||
this.weatherAPIKey,
|
this.weatherAPIKey,
|
||||||
@ -28,16 +26,8 @@ public class WeatherAPI implements WeatherDataAPI{
|
|||||||
days
|
days
|
||||||
)
|
)
|
||||||
).toURL();
|
).toURL();
|
||||||
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());
|
return JSONFetcher.fetch(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user