From e0cd0c376f8c71d8f69b195b3ea77e3179b423af Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 16 Jun 2024 21:53:05 +0200 Subject: [PATCH] current_weather and weather_forecast method fix for zips under length of 5 --- .../java/de/hs_mannheim/domain/System.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 9118362..eea4db5 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -347,13 +347,24 @@ public class System { try { HttpClient http_client = HttpClient.newHttpClient(); - - HttpRequest get_request = HttpRequest.newBuilder() + HttpRequest get_request; + + if(current_user.getZip().length()<5){ + get_request = HttpRequest.newBuilder() + .uri(new URI("https://api.openweathermap.org/data/2.5/weather?q=" + current_user.getHometown() + + ",de&appid=" + api_key + "&units=metric&lang=de")) + .GET() + .build(); + } + + else{ + get_request = HttpRequest.newBuilder() .uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip=" + current_user.getZip() + ",de&appid=" + api_key + "&units=metric&lang=de")) .GET() .build(); - + } + HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString()); JSONObject json = new JSONObject(get_response.body()); @@ -413,13 +424,23 @@ public class System { try { HttpClient http_client = HttpClient.newHttpClient(); - - HttpRequest get_request = HttpRequest.newBuilder() + HttpRequest get_request; + + if(destination_zip.length()<5){ + get_request = HttpRequest.newBuilder() + .uri(new URI("https://api.openweathermap.org/data/2.5/forecast?q=" + search(destination_zip).get(0).split(";")[1] + + ",de&appid=" + api_key + "&units=metric&lang=de")) + .GET() + .build(); + } + else{ + get_request = HttpRequest.newBuilder() .uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip=" + destination_zip + ",de&appid=" + api_key + "&units=metric&lang=de")) .GET() .build(); - + } + HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString()); JSONObject json = new JSONObject(get_response.body());