From 4aedfb286976561196d9d407eb63a0d421f0101e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Bab=C4=8D=C3=A1k?= <boris.babcak@kosickaakademia.sk> Date: Fri, 16 Feb 2024 11:30:00 +0100 Subject: [PATCH] added method to print temperature in city --- Country.json | 2 +- .../java/sk/kasv/babcak/worldx/APIcalls.java | 60 +++++++++++++++++++ .../java/sk/kasv/babcak/worldx/WorldApp.java | 11 +++- .../sk/kasv/babcak/worldx/WriterJSON.java | 17 ++++-- 4 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 src/main/java/sk/kasv/babcak/worldx/APIcalls.java diff --git a/Country.json b/Country.json index 1cb0385..e5819bf 100644 --- a/Country.json +++ b/Country.json @@ -1 +1 @@ -{"date":"2024-02-14","code3":"SVK","code2":"SK","capital":"Bratislava","cities":["Bratislava",448292,"Košice",241874,"Prešov",93977,"Poprad",51922,"Bardejov",27000,"Namestovo",8000,"Nitra",77000,"Trnava",92000],"name":"Slovakia"} \ No newline at end of file +{"date":"2024-02-16","code3":"SVK","code2":"SK","capital":"Bratislava","cities":[{"name":"Bratislava","population":448292},{"name":"Košice","population":241874},{"name":"Prešov","population":93977},{"name":"Poprad","population":51922},{"name":"Bardejov","population":27000},{"name":"Namestovo","population":8000},{"name":"Nitra","population":77000},{"name":"Trnava","population":92000}],"name":"Slovakia"} \ No newline at end of file diff --git a/src/main/java/sk/kasv/babcak/worldx/APIcalls.java b/src/main/java/sk/kasv/babcak/worldx/APIcalls.java new file mode 100644 index 0000000..fc1ee5c --- /dev/null +++ b/src/main/java/sk/kasv/babcak/worldx/APIcalls.java @@ -0,0 +1,60 @@ +package sk.kasv.babcak.worldx; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + + +public class APIcalls { + + private static final String API_KEY = "b1fd73144d85323925c1b2c6237d1dfc"; + + public double getTemperature(String name, String code){ + try { + URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=" + name + "&appid=" + API_KEY); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + conn.setConnectTimeout(5000); + conn.setReadTimeout(5000); + + int status = conn.getResponseCode(); + if (status == 200) { + try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { + StringBuilder content = new StringBuilder(); + String inputLine; + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + + String jsonResponse = content.toString(); + double temperature = extractTemperature(jsonResponse); + System.out.println("Temperature in " + name + ": " + temperature + " °C"); + return temperature; + } + } else { + System.out.println("Error: Unable to retrieve temperature. Status code: " + status); + } + conn.disconnect(); + } catch (IOException e) { + System.err.println("Error: Failed to fetch temperature data: " + e.getMessage()); + } + return 0; + } + + private double extractTemperature(String jsonResponse) { + try { + JSONObject jsonObject = new JSONObject(jsonResponse); + JSONObject mainObject = jsonObject.getJSONObject("main"); + double temperatureKelvin = mainObject.getDouble("temp"); + return temperatureKelvin - 273.15; + } catch (JSONException e) { + System.err.println("Error parsing temperature: " + e.getMessage()); + return Double.NaN; + } + } +} \ No newline at end of file diff --git a/src/main/java/sk/kasv/babcak/worldx/WorldApp.java b/src/main/java/sk/kasv/babcak/worldx/WorldApp.java index c28bf29..d3a210a 100644 --- a/src/main/java/sk/kasv/babcak/worldx/WorldApp.java +++ b/src/main/java/sk/kasv/babcak/worldx/WorldApp.java @@ -5,6 +5,7 @@ import java.sql.SQLException; public class WorldApp { public static void main(String[] args) throws SQLException { + try{ DataBase db = new DataBase(); WriterJSON writerJSON = new WriterJSON(); Country country = db.getCountry("SVK"); @@ -14,5 +15,11 @@ public class WorldApp { writerJSON.createJSONdocument("Country.json", country); country.printCity(); - } -} \ No newline at end of file + + APIcalls call = new APIcalls(); + call.getTemperature("Turkey", ""); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } \ No newline at end of file diff --git a/src/main/java/sk/kasv/babcak/worldx/WriterJSON.java b/src/main/java/sk/kasv/babcak/worldx/WriterJSON.java index cd25ab4..37ea78e 100644 --- a/src/main/java/sk/kasv/babcak/worldx/WriterJSON.java +++ b/src/main/java/sk/kasv/babcak/worldx/WriterJSON.java @@ -23,18 +23,27 @@ public class WriterJSON { private static JSONObject createJsonStructure(Country country) { JSONObject object = new JSONObject(); + APIcalls call = new APIcalls(); object.put("name", country.getName()); object.put("code2", country.getCode2()); object.put("code3", country.getCode3()); object.put("capital", country.getCapital()); + JSONArray jsonArray = new JSONArray(); + for (City city : country.getList()) { + JSONObject cityObject = new JSONObject(); + cityObject.put("name", city.getName()); + cityObject.put("population", city.getPopulation()); + + /* + double temperature = call.getTemperature(city.getName(), country.getCode2()); + cityObject.put("temperature", temperature); + */ - for(City city: country.getList()) { - jsonArray.put(city.getName()); - jsonArray.put(city.getPopulation()); + jsonArray.put(cityObject); } - object.put("cities",jsonArray); + object.put("cities", jsonArray); object.put("date", Util.getDate()); return object; -- GitLab