答案:开发Java天气查询应用需调用和风天气API获取JSON数据并解析展示。首先注册API获取密钥,通过HTTP客户端发送请求,使用JSONObject解析返回结果,输出城市气温、天气状况等信息,结合命令行输入实现简单交互,适合初学者掌握网络通信与数据处理基础。

开发一个简易的天气查询应用,核心在于获取网络API数据并解析展示。Java作为成熟的后端语言,非常适合实现这类HTTP请求与JSON处理任务。下面从需求分析到代码实现,一步步带你完成这个小项目。
1. 明确功能目标
我们要做一个命令行版的天气查询工具,用户输入城市名称,程序返回该城市的实时气温、天气状况、湿度等基本信息。
关键点包括:
- 调用第三方天气API(如和风天气、OpenWeatherMap)
- 发送HTTP请求获取JSON数据
- 解析JSON响应
- 格式化输出结果
2. 注册API并获取密钥
推荐使用和风天气或OpenWeatherMap,它们都提供免费层级的API服务。
立即学习“Java免费学习笔记(深入)”;
以和风天气为例:
- 注册账号并创建应用
- 获取你的API Key(形如 "abcdef1234567890")
- 查看文档中“实时天气”接口地址,例如:
https://devapi.qweather.com/v7/weather/now?location=101010100&key=YOUR_KEY
3. 添加必要依赖
使用 Maven 管理项目,在 pom.xml 中加入:
<dependencies>
<!-- HTTP 客户端 -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
</dependency>
<pre class="brush:php;toolbar:false;"><!-- JSON 解析 -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230618</version>
</dependency>4. 编写核心代码逻辑
创建 WeatherApp.java 文件:
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
<p>public class WeatherApp {</p><pre class="brush:php;toolbar:false;">private static final String API_KEY = "your_api_key_here"; // 替换为你的Key
private static final String BASE_URL = "https://devapi.qweather.com/v7/weather/now";
public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.out.println("请传入城市名称");
return;
}
String city = args[0];
String locationId = getLocationId(city); // 实际项目中需通过地理编码接口转换
String url = BASE_URL + "?location=" + locationId + "&key=" + API_KEY;
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet request = new HttpGet(url);
CloseableHttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == 200) {
String jsonStr = EntityUtils.toString(response.getEntity());
JSONObject json = new JSONObject(jsonStr);
JSONObject now = json.getJSONObject("now");
System.out.println("城市: " + city);
System.out.println("温度: " + now.getString("temp") + "℃");
System.out.println("天气: " + now.getString("text"));
System.out.println("体感温度: " + now.getString("feelsLike") + "℃");
System.out.println("风速: " + now.getString("windSpeed") + "km/h");
System.out.println("湿度: " + now.getString("humidity") + "%");
} else {
System.out.println("请求失败,状态码:" + response.getStatusLine().getStatusCode());
}
}
}
// 模拟根据城市名获取Location ID(真实场景应调用Geo API)
private static String getLocationId(String city) {
switch (city) {
case "北京": return "101010100";
case "上海": return "101020100";
case "广州": return "101280101";
default: return "101010100"; // 默认北京
}
}}
5. 运行与测试
编译并运行:
javac WeatherApp.java java WeatherApp 北京
输出示例:
城市: 北京温度: 23℃
天气: 晴
体感温度: 24℃
风速: 12km/h
湿度: 45%
6. 可扩展优化方向
- 集成地理编码API,支持中文城市名自动转Location ID
- 增加缓存机制避免频繁请求
- 封装成Web接口(Spring Boot)供前端调用
- 添加多语言、预报、空气质量等功能
- 使用 Gson 或 Jackson 替代原生 JSON 处理
基本上就这些。这个项目虽小,但涵盖了网络请求、JSON解析、异常处理等常见技能,适合初学者练手。只要理解流程,后续扩展也不难。关键是先跑通第一个版本。










