用JAVA做一个天气预报
时间: 2024-06-09 17:09:45 浏览: 14
要用Java实现天气预报功能,你需要准备以下内容:
1. 天气API:你需要从第三方天气服务商获取天气数据,例如心知天气、和风天气等。这些服务商一般都会提供API接口,方便你获取天气数据。
2. Java HTTP客户端:你需要用Java编写HTTP客户端,向天气API发送请求,并接收响应数据。
3. JSON解析库:获取到的天气数据一般是JSON格式的,你需要用Java的JSON解析库,将JSON数据解析为Java对象。
4. 用户界面:你需要用Java Swing或JavaFX等UI库,创建一个用户界面,方便用户输入城市名,显示天气数据等。
下面是一个简单的Java天气预报示例代码,仅供参考:
```java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.json.JSONArray;
import org.json.JSONObject;
public class WeatherApp extends JFrame implements ActionListener {
private JTextField cityField;
private JLabel weatherLabel;
public WeatherApp() {
super("天气预报");
setSize(400, 200);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
cityField = new JTextField();
panel.add(cityField, BorderLayout.CENTER);
JButton button = new JButton("查询");
button.addActionListener(this);
panel.add(button, BorderLayout.EAST);
weatherLabel = new JLabel();
panel.add(weatherLabel, BorderLayout.SOUTH);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
new WeatherApp();
}
@Override
public void actionPerformed(ActionEvent e) {
try {
String city = cityField.getText();
String urlStr = "https://api.seniverse.com/v3/weather/now.json?key=your_key&location=" + city;
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
conn.disconnect();
JSONObject json = new JSONObject(sb.toString());
JSONArray results = json.getJSONArray("results");
JSONObject result = results.getJSONObject(0);
JSONObject now = result.getJSONObject("now");
String text = now.getString("text");
String temperature = now.getString("temperature");
String weather = String.format("%s,温度:%s℃", text, temperature);
weatherLabel.setText(weather);
} catch (Exception ex) {
weatherLabel.setText("查询失败:" + ex.getMessage());
ex.printStackTrace();
}
}
}
```
上述示例代码使用了心知天气的API接口。你需要将代码中的`your_key`替换为你的API密钥,才能正常使用。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)