<meta charset="utf-8">
作者地址:http://www.itdecent.cn/p/365dd499af03
首先
說一下我們用的api的url,可以直接在瀏覽器上輸入查詢,但是會出現亂碼問題。
http://wthrcdn.etouch.cn/weather_mini?city=海淀
這里我們可以使用線上的測試工具。
https://www.sojson.com/convert/word2spell.html

image
注意,這里是get請求UTF-8,輸入地址,發(fā)送請求就可以查看天氣了。

image
java代碼部分
第一步,加入依賴 (pom文件)
<!--http必要-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<!--io可選-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<!--jackson可選-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
需要http依賴
創(chuàng)建一個類,main方法,以下是代碼部分就可以測試了。
//1.構建客戶端
CloseableHttpClient aDefault = HttpClients.createDefault();
//2.請求對象
HttpGet dpGet=new HttpGet("http://wthrcdn.etouch.cn/weather_mini?city=海淀");
//3.發(fā)送請求
CloseableHttpResponse closeableHttpResponse = aDefault.execute(dpGet);
//輸出內容
//獲取對象
String ent = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
System.out.println(ent);
最后的成品代碼,需要加入可選部分的依賴
這里添加的4個類將返回值以一種友好的方式呈現出來。
//Data類字段
Yesterday yesterday;
String city;
List<WeatherInfo> forecast;
String ganmao;
String wendu;
//WeatherForecast類字段
Data data;
String status;
String desc;
//WeatherInfo類字段
String date;
String high;
String fengli;
String low;
String fengxiang;
String type;
//Yesterday類字段
String date;
String high;
String fx;
String low;
String fl;
String type;
以下是代碼部分就可以測試了
package com.enjoy.weather.ui;
import com.enjoy.weather.model.WeatherForecast;
import com.enjoy.weather.model.WeatherInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
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 java.io.IOException;
import java.util.List;
import java.util.Scanner;
/**
* @ClassName WeatherApp
* @Description TODO
* @Author ly
* @Date 2021/3/22 15:23
* @Version 1.0
*/
public class WeatherApp {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.println("請輸入:");
String city = scanner.next();
//1.構建客戶端
CloseableHttpClient aDefault = HttpClients.createDefault();
//2.請求對象
HttpGet dpGet=new HttpGet("http://wthrcdn.etouch.cn/weather_mini?city="+city);
//3.發(fā)送請求
CloseableHttpResponse closeableHttpResponse = aDefault.execute(dpGet);
//輸出內容
//獲取對象
String ent = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
System.out.println(ent);
//解析返回值
ObjectMapper objectMapper = new ObjectMapper();
WeatherForecast weatherForecast = objectMapper.readValue(ent, WeatherForecast.class);
//解析對象的信息 獲取城市信息
if(weatherForecast.getStatus().equals("1002")){
System.out.println("您輸入的城市不存在");
return;
}
String cityName = weatherForecast.getData().getCity();
System.out.println("所在城市:"+cityName);
// 今后5天的天氣信息
List<WeatherInfo> forecast = weatherForecast.getData().getForecast();
for (WeatherInfo weatherInfo:forecast){
System.out.println("日期:"+weatherInfo.getDate());
System.out.println("天氣:"+weatherInfo.getType());
System.out.println("最高溫度:"+weatherInfo.getHigh());
System.out.println("最低溫度:"+weatherInfo.getLow());
System.out.println("風力:"+weatherInfo.getFengli());
System.out.println("風向:"+weatherInfo.getFengxiang());
System.out.println("-----------------");
}
}
}
結果案例輸出:

image.png