Die Funktionalität des WetterService wurde erfüllt

WetterService
danai 2024-06-14 13:07:42 +02:00
parent e9478289c5
commit 09a7b543a5
1 changed files with 72 additions and 0 deletions

View File

@ -1,6 +1,78 @@
package domain; package domain;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
public class WetterService { public class WetterService {
private static final String API_KEY = "fc01dfd47f80659f12512c295e80b201";
private static final HttpClient client = HttpClient.newHttpClient();
public static String getAktuellesWetter(String location) throws IOException, InterruptedException {
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&appid=" + API_KEY
+ "&units=metric";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new IOException("Unerwarteter Code " + response.statusCode());
}
JSONObject json = new JSONObject(response.body());
double temp = json.getJSONObject("main").getDouble("temp");
String weather = json.getJSONArray("weather").getJSONObject(0).getString("description");
return "Aktuelle Temperatur: " + temp + "°C, Wetter: " + weather;
}
public static String getWettervorhersage(String location) throws IOException, InterruptedException {
String url = "http://api.openweathermap.org/data/2.5/forecast?q=" + location + "&appid=" + API_KEY
+ "&units=metric";
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new IOException("Unerwarteter Code " + response.statusCode());
}
JSONObject json = new JSONObject(response.body());
Map<String, Double> tempByDay = new HashMap<>();
Map<String, String> weatherByDay = new HashMap<>();
LocalDate today = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
JSONArray forecastList = json.getJSONArray("list");
for (int i = 0; i < forecastList.length(); i++) {
JSONObject forecastEntry = forecastList.getJSONObject(i);
String dateTime = forecastEntry.getString("dt_txt");
String date = dateTime.split(" ")[0];
LocalDate forecastDate = LocalDate.parse(date, formatter);
if (forecastDate.isAfter(today) && !tempByDay.containsKey(date)) {
double temp = forecastEntry.getJSONObject("main").getDouble("temp");
String weather = forecastEntry.getJSONArray("weather").getJSONObject(0).getString("description");
tempByDay.put(date, temp);
weatherByDay.put(date, weather);
}
}
StringBuilder forecast = new StringBuilder("3-Tage-Wettervorhersage:\n");
tempByDay.keySet().stream().sorted().limit(3).forEach(date -> {
forecast.append(String.format("%s - Temperatur: %.2f°C, Wetter: %s\n", date, tempByDay.get(date),
weatherByDay.get(date)));
});
return forecast.toString();
}
} }