查询天气的接口
目前阶段我们是本地练习,没有后台数据提供,我们可以利用 聚合数据 提供的免费 api 找到自己想要的数据。聚合数据也提供免费的 api 来查询天气。
这里介绍另一个请求天气状况的接口:
http://wthrcdn.etouch.cn/WeatherApi?city=北京
后面 city 这个参数传入想查询的城市即可。
请求回来数据结构如下
可以看到返回的信息有:城市姓名、更新时间、温度、风力等信息,其中 yesterday 标签内是昨天的天气,forecast 标签内是未来天气的信息。
我们关注下 forecast 标签里的内容,结构如下:
代码实现
我们基于上一节的省份联动实现,点击相应城市查询城市天气,所以我们在上一节增加和修改以下代码:
1、增加一个用于描述响应的模型 Resp
public class Resp {
private String city;//北京
private String updatetime;//08:32
private String wendu;//26
private String shidu;//43%
private String fengxiang;//北风
private String sunrise_1;//05:38
private String sunset_1;//18:51
private List<Weather> forecast;
public Resp() {
}
public List<Weather> getForecast() {
return forecast;
}
public void setForecast(List<Weather> forecast) {
this.forecast = forecast;
}
public Resp(String city, String updatetime, String wendu, String shidu, String fengxiang, String sunrise_1, String sunset_1, List<Weather> forecast) {
this.city = city;
this.updatetime = updatetime;
this.wendu = wendu;
this.shidu = shidu;
this.fengxiang = fengxiang;
this.sunrise_1 = sunrise_1;
this.sunset_1 = sunset_1;
this.forecast = forecast;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getUpdatetime() {
return updatetime;
}
public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}
public String getWendu() {
return wendu;
}
public void setWendu(String wendu) {
this.wendu = wendu;
}
public String getShidu() {
return shidu;
}
public void setShidu(String shidu) {
this.shidu = shidu;
}
public String getFengxiang() {