Dernière activité 1718984504

Révision 00a343a8ebf198284fbca19628b57ea3e64dc4ea

WeatherApp.java Brut
1import java.io.BufferedReader;
2import java.io.IOException;
3import java.io.InputStreamReader;
4import java.net.HttpURLConnection;
5import java.net.URL;
6
7// go to https://openweathermap.org to see
8
9
10public class WeatherApp {
11
12 public static void main(String[] args) {
13 String apiKey = "YOUR_API_KEY"; // Replace with your OpenWeatherMap API key
14 String city = "New York"; // Replace with the desired city
15
16 try {
17 String apiUrl = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey;
18 URL url = new URL(apiUrl);
19
20 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
21 connection.setRequestMethod("GET");
22
23 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
24 String line;
25 StringBuilder response = new StringBuilder();
26
27 while ((line = reader.readLine()) != null) {
28 response.append(line);
29 }
30 reader.close();
31
32 // Parse and display the weather information
33 String weatherData = response.toString();
34 System.out.println("Weather Data:");
35 System.out.println(weatherData);
36
37 } catch (IOException e) {
38 e.printStackTrace();
39 }
40 }
41}
42