-
Notifications
You must be signed in to change notification settings - Fork 638
/
Copy pathOpenWeatherMap.java
38 lines (30 loc) · 1.26 KB
/
OpenWeatherMap.java
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class WeatherApp {
public static void main(String[] args) {
String apiKey = "YOUR_API_KEY"; // Replace with your OpenWeatherMap API key
String city = "New York"; // Replace with the desired city
try {
String apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey;
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// Parse and display the weather information
String weatherData = response.toString();
System.out.println("Weather Data:");
System.out.println(weatherData);
} catch (IOException e) {
e.printStackTrace();
}
}
}