kristofer hat die Gist bearbeitet . Zu Änderung gehen
1 file changed, 41 insertions
WeatherApp.java(Datei erstellt)
@@ -0,0 +1,41 @@ | |||
1 | + | import java.io.BufferedReader; | |
2 | + | import java.io.IOException; | |
3 | + | import java.io.InputStreamReader; | |
4 | + | import java.net.HttpURLConnection; | |
5 | + | import java.net.URL; | |
6 | + | ||
7 | + | // go to https://openweathermap.org to see | |
8 | + | ||
9 | + | ||
10 | + | public 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 | + | } |
Neuer
Älter