current_weather and weather_forecast method fix for zips under length of 5

bugfix_branch
Selim Eser 2024-06-16 21:53:05 +02:00
parent 0060368e0a
commit e0cd0c376f
1 changed files with 27 additions and 6 deletions

View File

@ -347,13 +347,24 @@ public class System {
try { try {
HttpClient http_client = HttpClient.newHttpClient(); HttpClient http_client = HttpClient.newHttpClient();
HttpRequest get_request;
HttpRequest get_request = HttpRequest.newBuilder()
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() .uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip=" + current_user.getZip()
+ ",de&appid=" + api_key + "&units=metric&lang=de")) + ",de&appid=" + api_key + "&units=metric&lang=de"))
.GET() .GET()
.build(); .build();
}
HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString()); HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString());
JSONObject json = new JSONObject(get_response.body()); JSONObject json = new JSONObject(get_response.body());
@ -413,13 +424,23 @@ public class System {
try { try {
HttpClient http_client = HttpClient.newHttpClient(); HttpClient http_client = HttpClient.newHttpClient();
HttpRequest get_request;
HttpRequest get_request = HttpRequest.newBuilder()
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 .uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip=" + destination_zip
+ ",de&appid=" + api_key + "&units=metric&lang=de")) + ",de&appid=" + api_key + "&units=metric&lang=de"))
.GET() .GET()
.build(); .build();
}
HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString()); HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString());
JSONObject json = new JSONObject(get_response.body()); JSONObject json = new JSONObject(get_response.body());