WeatherApp is a Java-based application that retrieves and displays global weather forecasts using the OpenWeatherMap API. Users can enter a city name, and the program returns current weather information for that location, including the city, country, time, and temperature.
- City Search: Allows users to input any city name.
- Live Data: Fetches real-time weather forecasts via the OpenWeatherMap API.
- Detailed Output: Displays city, country, forecast time, and temperature.
- OkHttp:Used for sending HTTP requests and retrieving weather data.
- Gson:Used for parsing the JSON response from the API.
- Go to the OpenWeatherMap website.
- Sign up and obtain a free API key. You will need this key to access the weather data.
Create a file named config.properties in the root directory of the project with the following content:
API_URL=https://api.openweathermap.org/data/2.5/forecast
API_KEY=你的API密钥
Note: Replace YOUR_API_KEY with the actual key you obtained from OpenWeatherMap.
1.Ensure you have the Java Development Kit (JDK) installed. 2.Download and include the OkHttp and Gson libraries in your classpath (via Maven, Gradle, or manual JARs). 3.Compile and run WeatherApp.java.
javac WeatherApp.java
java WeatherApp(Note: On Linux/Mac, replace ; with : in the classpath)
Upon running, the application will prompt for a city name.
Enter city name: London
Request URL: https://api.openweathermap.org/data/2.5/forecast?q=London&appid=your-api-key&units=metric
City: London, Country: GB
Time: 2024-12-09 15:00:00, Temperature: 8.5°C
Time: 2024-12-09 18:00:00, Temperature: 7.8°C
Time: 2024-12-09 21:00:00, Temperature: 7.1°C
...
This project is licensed under the MIT License. You are free to use, modify, and distribute this project, provided you include the original copyright and license notice.