fix: OpenMeteo implementation
This commit is contained in:
parent
e51b7de1e0
commit
4605b28d79
@ -1,5 +1,6 @@
|
|||||||
package eirb.pg203;
|
package eirb.pg203;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import eirb.pg203.utils.JSONFetcher;
|
import eirb.pg203.utils.JSONFetcher;
|
||||||
@ -19,15 +20,16 @@ public class OpenMeteo implements WeatherDataAPI {
|
|||||||
|
|
||||||
// https://www.nodc.noaa.gov/archive/arc0021/0002199/1.1/data/0-data/HTML/WMO-CODE/WMO4677.HTM
|
// https://www.nodc.noaa.gov/archive/arc0021/0002199/1.1/data/0-data/HTML/WMO-CODE/WMO4677.HTM
|
||||||
private JSONObject fetchWeather(int days, City city) throws IOException {
|
private JSONObject fetchWeather(int days, City city) throws IOException {
|
||||||
URL url = URI.create(
|
URL url = URI.create(
|
||||||
String.format(forecastBaseURL + "?latitude=%.2f&longitude=%.2f&forecast_days=%d&daily=" + dailyQuery,
|
String.format(forecastBaseURL + "?latitude=%.2f&longitude=%.2f&forecast_days=%d&daily=" + dailyQuery,
|
||||||
city.getCityCoords().getLat(),
|
city.getCityCoords().getLat(),
|
||||||
city.getCityCoords().getLon(),
|
city.getCityCoords().getLon(),
|
||||||
days
|
days
|
||||||
)
|
)
|
||||||
).toURL();
|
).toURL();
|
||||||
|
|
||||||
return JSONFetcher.fetch(url);
|
JSONArray jsonArray = JSONFetcher.fetchArray(url);
|
||||||
|
return jsonArray.getJSONObject(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Condition getConditionFromCode(int WMOCode) {
|
private static Condition getConditionFromCode(int WMOCode) {
|
||||||
|
@ -6,6 +6,7 @@ import java.io.InputStreamReader;
|
|||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class JSONFetcher {
|
public class JSONFetcher {
|
||||||
@ -20,6 +21,22 @@ public class JSONFetcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new JSONObject(result.toString());
|
return new JSONObject(result.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return new JSONArray(result.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user