在java中获取实时气象信息,可以通过调用气象api(如open-meteo、openweathermap等)实现。
方法1:使用 httpclient(java 11+)调用open-meteo api
open-meteo是一个免费的天气api,无需api密钥即可使用。
public static void main(string[] args) {
double latitude = 39.9042; // 北京纬度
double longitude = 116.4074; // 北京经度
string apiurl = string.format(
"https://api.open-meteo.com/v1/forecast?latitude=%.4f&longitude=%.4f¤t_weather=true",
latitude, longitude
);
httpclient client = httpclient.newhttpclient();
httprequest request = httprequest.newbuilder()
.uri(uri.create(apiurl))
.build();
try {
httpresponse<string> response = client.send(request, httpresponse.bodyhandlers.ofstring());
objectmapper mapper = new objectmapper();
jsonnode rootnode = mapper.readtree(response.body());
// 解析当前天气数据
jsonnode currentweather = rootnode.path("current_weather");
double temperature = currentweather.path("temperature").asdouble();
double windspeed = currentweather.path("windspeed").asdouble();
int winddirection = currentweather.path("winddirection").asint();
system.out.println("当前温度: " + temperature + "°c");
system.out.println("风速: " + windspeed + " km/h");
system.out.println("风向: " + winddirection + "°");
} catch (exception e) {
e.printstacktrace();
}
}输出示例:
当前温度: 25.3°c
风速: 12.5 km/h
风向: 180°
方法2:使用 okhttp 调用openweathermap api
openweathermap提供更详细的天气数据,但需要api密钥(免费注册)
使用到的依赖
<dependency>
<groupid>com.squareup.okhttp3</groupid>
<artifactid>okhttp</artifactid>
<version>4.10.0</version>
</dependency>
<dependency>
<groupid>com.fasterxml.jackson.core</groupid>
<artifactid>jackson-databind</artifactid>
<version>2.15.0</version>
</dependency>public static void main(string[] args) {
string apikey = "your_api_key"; // 替换为你的api密钥
string city = "beijing";
string apiurl = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apikey + "&units=metric";
okhttpclient client = new okhttpclient();
request request = new request.builder()
.url(apiurl)
.build();
try {
response response = client.newcall(request).execute();
string responsebody = response.body().string();
objectmapper mapper = new objectmapper();
jsonnode rootnode = mapper.readtree(responsebody);
// 解析天气数据
double temperature = rootnode.path("main").path("temp").asdouble();
double humidity = rootnode.path("main").path("humidity").asdouble();
string weatherdesc = rootnode.path("weather").get(0).path("description").astext();
system.out.println("当前温度: " + temperature + "°c");
system.out.println("湿度: " + humidity + "%");
system.out.println("天气状况: " + weatherdesc);
} catch (exception e) {
e.printstacktrace();
}
}输出示例:
当前温度: 26.5°c
湿度: 65%
天气状况: 多云
方法3:使用 spring webclient
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-webflux</artifactid>
</dependency>public class weatherservice {
private final webclient webclient;
public weatherservice() {
this.webclient = webclient.create("https://api.open-meteo.com");
}
public mono<string> getcurrentweather(double latitude, double longitude) {
return webclient.get()
.uri(uribuilder -> uribuilder
.path("/v1/forecast")
.queryparam("latitude", latitude)
.queryparam("longitude", longitude)
.queryparam("current_weather", true)
.build())
.retrieve()
.bodytomono(string.class);
}
public static void main(string[] args) {
weatherservice service = new weatherservice();
service.getcurrentweather(39.9042, 116.4074)
.subscribe(system.out::println);
}
}输出示例:
{
"latitude": 39.9042,
"longitude": 116.4074,
"current_weather": {
"temperature": 25.3,
"windspeed": 12.5,
"winddirection": 180
}
}
总结
| 方法 | 适用场景 | 是否需要api密钥 | 推荐指数 |
|---|---|---|---|
| open-meteo | 免费、简单、全球数据 | ❌ 不需要 | ⭐⭐⭐⭐ |
| openweathermap | 更详细数据,但需注册 | ✅ 需要 | ⭐⭐⭐ |
| spring webclient | spring boot项目,异步支持 | 视api而定 | ⭐⭐⭐⭐ |
到此这篇关于java中获取实时气象信息的3种方法的文章就介绍到这了,更多相关java获取实时气象信息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论