|

楼主 |
发表于 2014-2-7 14:32:25
|
显示全部楼层
相信大家对于天气预报功能比较感兴趣,这里做点简短的补充:
时钟使用的是MSN的开放天气服务API。用过Windows7系统的应该看到系统自带的”小工具“功能,内”天气“侧边栏用的就是这个API。
搜索某地的cityid:http://weather.service.msn.com/f ... a&weasearchstr=地区汉语拼音
注意搜索时必须使用该地名的汉语拼音,如搜索丽江则:
http://weather.service.msn.com/f ... easearchstr=Lijiang
该城市的cityid见字段weatherlocationcode
根据cityid读取该地的天气:http://weather.service.msn.com/d ... wealocations=cityid
API返回的是XML格式的数据单(测试时可以用浏览器打开)。首先对域名weather.service.msn.com进行动态解析,建立TCP通道,然后根据超文本传输协议将XML请求下来,再解析相关字段即可。字段skytextday是天气状况的英文表示,比如cloud,sunny等。
在这里附上天气代码的中文含义表:
26, 27, 28 "阴"
35, 39, 45, 46 "小雨"
19, 20, 21, 22 "雾"
29, 30, 33 "多云"
5, 13, 14, 15, 16, 18, 25, 41, 42, 43 "雪"
1, 2, 3, 4, 37, 38, 47 "雷雨"
31, 32, 34, 36, 44 "晴"
23, 24 "中到大风"
9, 10, 11, 12, 40 "中到大雨"
6, 7, 8, 17 "冰雹"
下面是返回数据的几个例子供参考:
find.aspx返回实例:
<weatherdata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<weather weatherlocationcode="wc:CHXX0382" weatherlocationname="Lijiang, CHN" zipcode="" weatherfullname="Lijiang, Yunnan, China" searchlocation="Lijiang, Yunnan, China" searchdistance="0" searchscore="0.95" url="http://local.msn.com/worldweather.aspx?eid=8084904&q=Lijiang-CHN&ctsrc=vista" imagerelativeurl="http://wst.s-msn.com/i/en-us/" degreetype="F" provider="Foreca" isregion="False" region="" alert="" searchresult="Lijiang, Yunnan, China" lat="23.5756492614746" lon="101.984260559082" entityid="8084904">
<current temperature="70" skycode="32" skytext="Clear"/>
</weather>
</weatherdata>
data.aspx返回实例:
<weatherdata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<weather weatherlocationcode="wc:CHXX0382" weatherlocationname="Lijiang, CHN" zipcode="" url="http://local.msn.com/worldweather.aspx?eid=8084904&q=Lijiang-CHN&ctsrc=vista" imagerelativeurl="http://wst.s-msn.com/i/en-us/" degreetype="F" provider="Foreca" attribution="Data provided by Foreca" attribution2="© Foreca" lat="23.5827332" long="101.993515" timezone="8" alert="" entityid="8084904" encodedlocationname="Lijiang%2c+CHN">
<current temperature="70" skycode="32" skytext="Clear" date="2014-02-07" observationtime="11:00:00" observationpoint="Yuanjiang" feelslike="70" humidity="53" winddisplay="2 mph ENE" day="Friday" shortday="Fri" windspeed="2"/>
<forecast low="56" high="86" skycodeday="32" skytextday="Clear" date="2014-02-07" day="Friday" shortday="Fri" precip="2"/>
<forecast low="55" high="85" skycodeday="34" skytextday="Fair" date="2014-02-08" day="Saturday" shortday="Sat" precip="2"/>
<forecast low="57" high="87" skycodeday="30" skytextday="Partly Cloudy" date="2014-02-09" day="Sunday" shortday="Sun" precip="2"/>
<forecast low="57" high="88" skycodeday="30" skytextday="Partly Cloudy" date="2014-02-10" day="Monday" shortday="Mon" precip="10"/>
<forecast low="58" high="88" skycodeday="30" skytextday="Partly Cloudy" date="2014-02-11" day="Tuesday" shortday="Tue" precip="25"/>
<toolbar timewindow="60" minversion="1.0.1965.0"/>
</weather>
</weatherdata> |
评分
-
2
查看全部评分
-
|