本文将介绍两种免费获取天气预报信息的方式(天气信息来源于国家气象局,还是比较权威及时的):

(1)JSON 类型接口

JSON 类型的接口返回的数据又有三种形式,具体各种接口地址以及返回信息形式参见如下:

(1)接口地址:http://www.weather.com.cn/data/sk/101180605.html

返回信息形式:

{"weatherinfo":{"city":"新县","cityid":"101180605","temp":"31","WD":"北风","WS":"2级","SD":"53%","WSE":"2","time":"15:00","isRadar":"0","Radar":""}}

(2)接口地址:http://www.weather.com.cn/data/cityinfo/101180605.html

返回信息形式:

{"weatherinfo":{"city":"新县","cityid":"101180605","temp1":"32℃","temp2":"22℃","weather":"晴转多云","img1":"d0.gif","img2":"n1.gif","ptime":"11:00"}}

(3)接口地址:http://m.weather.com.cn/data/101180605.html

返回信息形式(信息包括近七天的天气数据):

{"weatherinfo":{"city":"新县","city_en":"xinxian","date_y":"2012年8月30日","date":"","week":"星期四","fchh":"11","cityid":"101180605","temp1":"32℃~22℃","temp2":"31℃~23℃","temp3":"30℃~23℃","temp4":"29℃~20℃","temp5":"27℃~20℃","temp6":"27℃~21℃","tempF1":"89.6℉~71.6℉","tempF2":"87.8℉~73.4℉","tempF3":"86℉~73.4℉","tempF4":"84.2℉~68℉","tempF5":"80.6℉~68℉","tempF6":"80.6℉~69.8℉","weather1":"晴转多云","weather2":"晴转阴","weather3":"阴转阵雨","weather4":"阵雨","weather5":"大雨转中雨","weather6":"多云","img1":"0","img2":"1","img3":"0","img4":"2","img5":"2","img6":"3","img7":"3","img8":"99","img9":"9","img10":"8","img11":"1","img12":"99","img_single":"0","img_title1":"晴","img_title2":"多云","img_title3":"晴","img_title4":"阴","img_title5":"阴","img_title6":"阵雨","img_title7":"阵雨","img_title8":"阵雨","img_title9":"大雨","img_title10":"中雨","img_title11":"多云","img_title12":"多云","img_title_single":"晴","wind1":"微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风","fx1":"微风","fx2":"微风","fl1":"小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级","index":"炎热","index_d":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等清凉夏季服装。","index48":"炎热","index48_d":"天气炎热,建议着短衫、短裙、短裤、薄型T恤衫、敞领短袖棉衫等清凉夏季服装。","index_uv":"强","index48_uv":"强","index_xc":"较适宜","index_tr":"适宜","index_co":"较不舒适","st1":"31","st2":"22","st3":"30","st4":"22","st5":"31","st6":"21","index_cl":"较适宜","index_ls":"极适宜","index_ag":"不易发"}}

如何调用以上天气信息接口呢?

其实调用以上天气信息接口,对于不同的城市不同的地方是url中的数字部分。以上url部分中的101180605代表是“新县”这个地方的代码,以下简称为cityid。因此只需要将以上url中数字部分换成相应的cityid即可获取对应地区的天气信息了。那么如何获取各地区的cityid呢?

以“河南省 – 信阳市 – 新县”为例进行说明:

首先我们需要获取河南省的cityid,

获取省级代码,url地址为:http://www.weather.com.cn/data/list3/city.xml?level=1(注意这个地址打开后可能提示xml文档错误什么什么的,不用管它,直接看该页面的源代码,查找对应城市的cityid值即可)

这里我们通过获取到的数据得到了河南省的cityid为:18,下面我们需要获取河南省下信阳市的cityid。

获取城市代码,这里获取河南省以下城市的cityid的url地址为:http://www.weather.com.cn/data/list3/city18.xml?level=2

通过以上链接我们获取到了信阳市的cityid为1806,下面我们要获取信阳市下的新县的cityid。

获取区域代码,同上这里获取信阳市下所有cityid的url地址为:http://www.weather.com.cn/data/list3/city1806.xml?level=3

通过以上链接我们最终得到新县的cityid为:180605

至此,新县地区cityid已经获取到,那么对应的新县的天气预报请求rul为:http://m.weather.com.cn/data/101180605.html(注意数字前面的101不可去掉,代表中国的cityid)

(2)XML 类型接口:

获取全国天气的请求URL地址:http://flash.weather.com.cn/wmaps/xml/china.xml

这个url获取的结果列出所有的省的天气信息,结果信息中的pyName字段是各个省XML的文件名,比如河南省的就是henan,对应的天气请求URL为:http://flash.weather.com.cn/wmaps/xml/henan.xml

同样,上述请求将获取河南省所有市的天气数据,通过其中的pyName字段同样可以获取该市下面所有县的天气数据。

数据信息中的tmp1是最低温度,tmp2是最高温度。

至此两种获取天气预报的方式就介绍完了,读者可以根据自己的需要选择合适的方式获取天气预报信息。