WeatherApp.java
                        
                             · 1.3 KiB · Java
                        
                    
                    
                      
                        Eredeti
                      
                      
                        
                          
                        
                    
                    
                
                
            import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
// go to https://openweathermap.org to see
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();
        }
    }
}
                | 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 | } | 
| 42 |