https://blog.csdn.net/weixin_38959210/article/details/83515783
首先进上面这个链接的文档里面,获取到JAVA调用HTTP接口的方法。
我们用的这个http接口获取天气情况"http://t.weather.sojson.com/api/weather/city/101170301",后面的"101170301"为城市编号。
各城市编号连接下载地址"https://download.csdn.net/download/weixin_38959210/10793554";
@RequestMapping(value = "/tianqi.do")
public JSONObject tianqi(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("UTF-8");
System.out.println("/tianqi.do打印出的东西:"+cn.temptation.web.tianqi.sendGet("http://t.weather.sojson.com/api/weather/city/101170301"));
return (JSONObject) JSON.parse(cn.temptation.web.tianqi.sendGet("http://t.weather.sojson.com/api/weather/city/101170301"));
}
以上为后台代码:
前台html代码为:
城市
更新时间
湿度
pm2.5
pm10
空气质量
温度
空气指数
js代码为:
$(function(){
function tianqi(){
$.ajax({
url: "tianqi.do",
type: "POST",
datatype: "json",
contentType: "application/json",
success: function(data){
$("#time").html(data.time);
$("#city").html(data.cityInfo.city);
$("#updateTime").html(data.cityInfo.updateTime);
$("#shidu").html(data.data.shidu);
$("#pm25").html(data.data.pm25);
$("#pm10").html(data.data.pm10);
$("#quality").html(data.data.quality);
$("#wendu").html(data.data.wendu);
$("#ganmao").html(data.data.ganmao);
},
error:function(){
alert("获取数据失败");
}
});
}
下面这个就是显示效果:
看完文章了可以试试下面这个接口:
http://i.tianqi.com/index.php?c=code&id=12&icon=1&num=5