From 1613a137f020326d327446fecce48b5f2328d876 Mon Sep 17 00:00:00 2001 From: CodelineAtyab Date: Sat, 11 Apr 2026 14:50:39 +0400 Subject: [PATCH 1/3] Added ok http client and GSON --- .gitignore | 7 +- pom.xml | 16 +++ .../example/fromatyab/MuscatWeatherApp.java | 115 ++++++++++++++++++ 3 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/example/fromatyab/MuscatWeatherApp.java diff --git a/.gitignore b/.gitignore index 0aeecdbc..4d3ae4b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ -.idea/ -target/ -plag_test_tool/ +.idea/ +.vscode/ +target/ +plag_test_tool/ .DS_Store.DS_Store .DS_Store diff --git a/pom.xml b/pom.xml index 9baca69e..c0882aa8 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,22 @@ UTF-8 + + + + com.squareup.okhttp3 + okhttp + 4.12.0 + + + + + com.google.code.gson + gson + 2.10.1 + + + diff --git a/src/main/java/org/example/fromatyab/MuscatWeatherApp.java b/src/main/java/org/example/fromatyab/MuscatWeatherApp.java new file mode 100644 index 00000000..862e0bcf --- /dev/null +++ b/src/main/java/org/example/fromatyab/MuscatWeatherApp.java @@ -0,0 +1,115 @@ +package org.example.fromatyab; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import java.io.IOException; + +/** + * A simple weather application that fetches and displays current weather for Muscat. + * This program demonstrates how to use popular Java libraries for HTTP requests and JSON parsing. + * + * Real-world analogy: Think of this like ordering food delivery - + * you send a request (OkHttp), get a response with details (JSON), and read the information (Gson). + * + * Libraries used: + * - OkHttp: Makes HTTP requests simple (like Python's 'requests' library) + * - Gson: Parses JSON data easily (converts text to Java objects) + */ +public class MuscatWeatherApp { + + // Free weather API for Muscat (Open-Meteo - no API key needed!) + // Latitude and longitude for Muscat, Oman + private static final String WEATHER_API_URL = + "https://api.open-meteo.com/v1/forecast?latitude=23.588&longitude=58.3829¤t_weather=true"; + + /** + * Main method to run the weather application. + * + * @param args command line arguments (not used) + */ + public static void main(String[] args) { + System.out.println("=== Muscat Weather Application ===\n"); + + try { + String weatherJson = fetchWeatherData(); + displayWeather(weatherJson); + } catch (IOException e) { + System.out.println("Error fetching weather data: " + e.getMessage()); + System.out.println("\nTip: Make sure you have an internet connection!"); + } + } + + /** + * Fetches weather data from the API using OkHttp. + * + * Real-world analogy: This is like sending a text message to a weather service + * and waiting for their reply with the weather information. + * + * @return weather data as a JSON string + * @throws IOException if the request fails + */ + private static String fetchWeatherData() throws IOException { + // Step 1: Create an HTTP client (like installing a web browser) + OkHttpClient client = new OkHttpClient(); + + // Step 2: Build the request (like typing a website URL) + Request request = new Request.Builder() + .url(WEATHER_API_URL) + .build(); + + // Step 3: Execute the request and get response (like clicking "Go" in the browser) + try (Response response = client.newCall(request).execute()) { + + // Step 4: Check if the request was successful + if (!response.isSuccessful()) { + throw new IOException("Unexpected response code: " + response); + } + + // Step 5: Return the response body as a string + return response.body().string(); + } + } + + /** + * Displays the weather data by parsing JSON and showing it in a readable format. + * + * Real-world analogy: This is like reading a receipt - + * the receipt has structured information (JSON), and we extract what we need. + * + * @param weatherJson the weather information in JSON format + */ + private static void displayWeather(String weatherJson) { + // Step 1: Print the raw JSON response + System.out.println("Raw JSON Response:"); + System.out.println(weatherJson); + System.out.println("\n" + "=".repeat(60) + "\n"); + + // Step 2: Parse the JSON using Gson (convert text to Java object) + Gson gson = new Gson(); + JsonObject jsonObject = gson.fromJson(weatherJson, JsonObject.class); + + // Step 3: Extract specific data from the JSON + JsonObject currentWeather = jsonObject.getAsJsonObject("current_weather"); + double temperature = currentWeather.get("temperature").getAsDouble(); + double windSpeed = currentWeather.get("windspeed").getAsDouble(); + String time = currentWeather.get("time").getAsString(); + + + + + + // Step 4: Display the parsed data in a user-friendly way + System.out.println("Formatted Weather Information:"); + System.out.println("Location: Muscat, Oman"); + System.out.println("Time: " + time); + System.out.println("Temperature: " + temperature + "°C"); + System.out.println("Wind Speed: " + windSpeed + " km/h"); + System.out.println("\n" + "=".repeat(60)); + System.out.println("Data source: Open-Meteo API (free weather data)"); + System.out.println("Libraries: OkHttp (HTTP client) + Gson (JSON parser)"); + } +} From df1a6f83af64a88712bd7317c57ed5d17296e1d3 Mon Sep 17 00:00:00 2001 From: CodelineAtyab Date: Sat, 11 Apr 2026 16:07:51 +0400 Subject: [PATCH 2/3] Fixed indentation --- .../java/org/example/fromatyab/MuscatWeatherApp.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/example/fromatyab/MuscatWeatherApp.java b/src/main/java/org/example/fromatyab/MuscatWeatherApp.java index 862e0bcf..0e293bec 100644 --- a/src/main/java/org/example/fromatyab/MuscatWeatherApp.java +++ b/src/main/java/org/example/fromatyab/MuscatWeatherApp.java @@ -84,9 +84,9 @@ private static String fetchWeatherData() throws IOException { */ private static void displayWeather(String weatherJson) { // Step 1: Print the raw JSON response - System.out.println("Raw JSON Response:"); - System.out.println(weatherJson); - System.out.println("\n" + "=".repeat(60) + "\n"); + System.out.println("Raw JSON Response:"); + System.out.println(weatherJson); + System.out.println("\n" + "=".repeat(60) + "\n"); // Step 2: Parse the JSON using Gson (convert text to Java object) Gson gson = new Gson(); @@ -99,8 +99,6 @@ private static void displayWeather(String weatherJson) { String time = currentWeather.get("time").getAsString(); - - // Step 4: Display the parsed data in a user-friendly way System.out.println("Formatted Weather Information:"); @@ -111,5 +109,8 @@ private static void displayWeather(String weatherJson) { System.out.println("\n" + "=".repeat(60)); System.out.println("Data source: Open-Meteo API (free weather data)"); System.out.println("Libraries: OkHttp (HTTP client) + Gson (JSON parser)"); + + + } } From ceb8378bb78b894f6dc9e94584692e9e2c30b5e4 Mon Sep 17 00:00:00 2001 From: CodelineAtyab Date: Sat, 11 Apr 2026 16:15:36 +0400 Subject: [PATCH 3/3] Removed blank lines --- src/main/java/org/example/fromatyab/MuscatWeatherApp.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/org/example/fromatyab/MuscatWeatherApp.java b/src/main/java/org/example/fromatyab/MuscatWeatherApp.java index 0e293bec..8051aa03 100644 --- a/src/main/java/org/example/fromatyab/MuscatWeatherApp.java +++ b/src/main/java/org/example/fromatyab/MuscatWeatherApp.java @@ -97,8 +97,6 @@ private static void displayWeather(String weatherJson) { double temperature = currentWeather.get("temperature").getAsDouble(); double windSpeed = currentWeather.get("windspeed").getAsDouble(); String time = currentWeather.get("time").getAsString(); - - // Step 4: Display the parsed data in a user-friendly way System.out.println("Formatted Weather Information:");