-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherApp.java
More file actions
56 lines (44 loc) · 1.79 KB
/
WeatherApp.java
File metadata and controls
56 lines (44 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class WeatherApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter city: ");
String city = scanner.nextLine();
try {
String apiKey = "5e7a41eb1daaf4b458925b95128a75ba"; // replace later
String urlString = "https://api.openweathermap.org/data/2.5/weather?q="
+ city + "&appid=" + apiKey + "&units=metric";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// Print raw JSON (for now)
// Convert response to JSON
org.json.JSONObject json = new org.json.JSONObject(response.toString());
// Extract values
double temp = json.getJSONObject("main").getDouble("temp");
int humidity = json.getJSONObject("main").getInt("humidity");
String weather = json.getJSONArray("weather")
.getJSONObject(0)
.getString("description");
// Print nicely
System.out.println("\n🌦 Weather in " + city);
System.out.println("Temperature: " + temp + "°C");
System.out.println("Humidity: " + humidity + "%");
System.out.println("Condition: " + weather);
} catch (Exception e) {
e.printStackTrace();
}
}
}