利用YAHOO公开API做天气预报Web服务
2009-03-09 17:31
633 查看
学了一段时间的Web服务,今天做了一个Web服务,利用YAHOO的公开天气API做自己的Web服务,主要是想练练手。现在把过程和心得分享给大家。
求教:这个Web服务还有个不完善的地方,Web服务的 CityNameToCityNum方法,这个最重要,他是把省会和直辖市的名字转换为编号,因为YAHOO传的参数不是城市名字的区号,全是自己的,而我又想不到更好的获得YAHOO城市对应的编号的方法,所以就创建了HASHTABLE存储了中国的各个省会城市和直辖市,希望有高手提出更好的方法,能不用这样,直接找YAHOO获取编号,提取更多的城市,而不用把所有的中国所有的城市全写在HASHTABLE里。
[b]Web服务地址:http://www.h2bbs.com/Weather/Weather.asmx[/b]
原理:
在Yahoo的Developer Network
http://developer.yahoo.com/weather/
详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文主要是利用它的API做Web服务,其它的应用由网友们自由发挥
首先了解Yahoo Weather Api的RSS Response格式(这是下午我查我家银川天气时返回的RSS):
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Yinchuan, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*http://kb.cnblogs.com/page/42993/%3C/span%3E%3Cspan%20style=" target=_blank>http://kb.cnblogs.com/page/42993/%3C/span%3E%3Cspan%20style=</link>
<description>Yahoo! Weather for Yinchuan, CH</description>
<language>en-us</language>
<lastBuildDate>Tue, 14 Oct 2008 11:00 am CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Yinchuan" region="" country="CH"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="56" direction="360" speed="4" />
<yweather:atmosphere humidity="56" visibility="999" pressure="" rising="0" />
<yweather:astronomy sunrise="7:03 am" sunset="6:19 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Yinchuan, CH at 11:00 am CST</title>
<geo:lat>38.48</geo:lat>
<geo:long>106.22</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*http://kb.cnblogs.com/page/42993/%3C/span%3E%3Cspan%20style=" target=_blank>http://kb.cnblogs.com/page/42993/%3C/span%3E%3Cspan%20style=</link>
<pubDate>Tue, 14 Oct 2008 11:00 am CST</pubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="56" date=
"Tue, 14 Oct 2008 11:00 am CST" />
<description>
<![CDATA[
<img src="
//l.yimg.com/us.yimg.com/i/us/we/52/28.gif" target=_blank><b>Current Conditions:</b><br />
Mostly Cloudy, 56 F<BR />
<BR /><b>Forecast:</b><BR />
Tue - Mostly Cloudy. High: 68 Low: 47<br />
Wed - Partly Cloudy. High: 70 Low: 44<br />
<br />
<a href="Full'>http://us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*http://weather.yahoo.com/forecast/CHXX0259_f.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]>
</description>
<yweather:forecast day="Tue" date="14 Oct 2008" low="47" high="68" text
="Mostly Cloudy" code="28" />
<yweather:forecast day="Wed" date="15 Oct 2008" low="44" high="70" text
="Partly Cloudy" code="30" />
<guid isPermaLink="false">CHXX0259_2008_10_14_11_00_CST</guid>
</item>
</channel>
</rss>
<!-- api5.weather.sp1.yahoo.com compressed/chunked Mon Oct 13 22:30:39 PDT
2008 -->
其中最重要的是后面的几行,查询当天和第二天的天气情况,我们要获取的天气信息就在里面,代码如下:
<yweather:forecast day="Tue" date="14 Oct 2008" low="47" high="68" text
="Mostly Cloudy" code="28" />
<yweather:forecast day="Wed" date="15 Oct 2008" low="44" high="70" text
="Partly Cloudy" code="30" />
我们所需要用到的Node是/rss/channel/item/yweather: forecast;这个节点里的东西是我们需要的。
知道了这些,我们接下来要做的就是在VS中建立WEB服务,并添加下面的代码:
namespace Weather
<?xml version="1.0" encoding="utf-8" ?>
- <DataSet xmlns="http://www.h2bbs.com/WebService/Weather.asmx">
- <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:
UseCurrentLocale="true">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="Weather">
- <xs:complexType>
- <xs:sequence>
<xs:element name="Date" type="xs:string" minOccurs="0" />
<xs:element name="Week" type="xs:string" minOccurs="0" />
<xs:element name="Weather" type="xs:string" minOccurs="0" />
<xs:element name="Tlow" type="xs:string" minOccurs="0" />
<xs:element name="Thigh" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
- <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
- <NewDataSet xmlns="">
- <Weather diffgr:id="Weather1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<Date>2008年10月14日</Date>
<Week>星期二(Tue)</Week>
<Weather>Clear</Weather>
<Tlow>16.1℃</Tlow>
<Thigh>26.7℃</Thigh>
</Weather>
- <Weather diffgr:id="Weather2" msdata:rowOrder="1" diffgr:hasChanges=
"inserted">
<Date>2008年10月15日</Date>
<Week>星期三(Wed)</Week>
<Weather>Sunny</Weather>
<Tlow>16.7℃</Tlow>
<Thigh>28.3℃</Thigh>
</Weather>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
写完了!最后希望能给大家带来收获!
求教:这个Web服务还有个不完善的地方,Web服务的 CityNameToCityNum方法,这个最重要,他是把省会和直辖市的名字转换为编号,因为YAHOO传的参数不是城市名字的区号,全是自己的,而我又想不到更好的获得YAHOO城市对应的编号的方法,所以就创建了HASHTABLE存储了中国的各个省会城市和直辖市,希望有高手提出更好的方法,能不用这样,直接找YAHOO获取编号,提取更多的城市,而不用把所有的中国所有的城市全写在HASHTABLE里。
[b]Web服务地址:http://www.h2bbs.com/Weather/Weather.asmx[/b]
原理:
在Yahoo的Developer Network
http://developer.yahoo.com/weather/
详细地介绍了Yahoo天气预报的API调用方法,这里用C#来实现,本文主要是利用它的API做Web服务,其它的应用由网友们自由发挥
首先了解Yahoo Weather Api的RSS Response格式(这是下午我查我家银川天气时返回的RSS):
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Yinchuan, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*http://kb.cnblogs.com/page/42993/%3C/span%3E%3Cspan%20style=" target=_blank>http://kb.cnblogs.com/page/42993/%3C/span%3E%3Cspan%20style=</link>
<description>Yahoo! Weather for Yinchuan, CH</description>
<language>en-us</language>
<lastBuildDate>Tue, 14 Oct 2008 11:00 am CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Yinchuan" region="" country="CH"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="56" direction="360" speed="4" />
<yweather:atmosphere humidity="56" visibility="999" pressure="" rising="0" />
<yweather:astronomy sunrise="7:03 am" sunset="6:19 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Yinchuan, CH at 11:00 am CST</title>
<geo:lat>38.48</geo:lat>
<geo:long>106.22</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*http://kb.cnblogs.com/page/42993/%3C/span%3E%3Cspan%20style=" target=_blank>http://kb.cnblogs.com/page/42993/%3C/span%3E%3Cspan%20style=</link>
<pubDate>Tue, 14 Oct 2008 11:00 am CST</pubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="56" date=
"Tue, 14 Oct 2008 11:00 am CST" />
<description>
<![CDATA[
<img src="
//l.yimg.com/us.yimg.com/i/us/we/52/28.gif" target=_blank><b>Current Conditions:</b><br />
Mostly Cloudy, 56 F<BR />
<BR /><b>Forecast:</b><BR />
Tue - Mostly Cloudy. High: 68 Low: 47<br />
Wed - Partly Cloudy. High: 70 Low: 44<br />
<br />
<a href="Full'>http://us.rd.yahoo.com/dailynews/rss/weather/Yinchuan__CH/*http://weather.yahoo.com/forecast/CHXX0259_f.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]>
</description>
<yweather:forecast day="Tue" date="14 Oct 2008" low="47" high="68" text
="Mostly Cloudy" code="28" />
<yweather:forecast day="Wed" date="15 Oct 2008" low="44" high="70" text
="Partly Cloudy" code="30" />
<guid isPermaLink="false">CHXX0259_2008_10_14_11_00_CST</guid>
</item>
</channel>
</rss>
<!-- api5.weather.sp1.yahoo.com compressed/chunked Mon Oct 13 22:30:39 PDT
2008 -->
其中最重要的是后面的几行,查询当天和第二天的天气情况,我们要获取的天气信息就在里面,代码如下:
<yweather:forecast day="Tue" date="14 Oct 2008" low="47" high="68" text
="Mostly Cloudy" code="28" />
<yweather:forecast day="Wed" date="15 Oct 2008" low="44" high="70" text
="Partly Cloudy" code="30" />
我们所需要用到的Node是/rss/channel/item/yweather: forecast;这个节点里的东西是我们需要的。
知道了这些,我们接下来要做的就是在VS中建立WEB服务,并添加下面的代码:
namespace Weather
<?xml version="1.0" encoding="utf-8" ?>
- <DataSet xmlns="http://www.h2bbs.com/WebService/Weather.asmx">
- <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:
UseCurrentLocale="true">
- <xs:complexType>
- <xs:choice minOccurs="0" maxOccurs="unbounded">
- <xs:element name="Weather">
- <xs:complexType>
- <xs:sequence>
<xs:element name="Date" type="xs:string" minOccurs="0" />
<xs:element name="Week" type="xs:string" minOccurs="0" />
<xs:element name="Weather" type="xs:string" minOccurs="0" />
<xs:element name="Tlow" type="xs:string" minOccurs="0" />
<xs:element name="Thigh" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
- <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
- <NewDataSet xmlns="">
- <Weather diffgr:id="Weather1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<Date>2008年10月14日</Date>
<Week>星期二(Tue)</Week>
<Weather>Clear</Weather>
<Tlow>16.1℃</Tlow>
<Thigh>26.7℃</Thigh>
</Weather>
- <Weather diffgr:id="Weather2" msdata:rowOrder="1" diffgr:hasChanges=
"inserted">
<Date>2008年10月15日</Date>
<Week>星期三(Wed)</Week>
<Weather>Sunny</Weather>
<Tlow>16.7℃</Tlow>
<Thigh>28.3℃</Thigh>
</Weather>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
写完了!最后希望能给大家带来收获!
相关文章推荐
- Web Service学习笔记:利用YAHOO公开API做天气预报Web服务
- 通过Yahoo+Search+API访问Web服务实现自己的搜索引擎
- 【转】 使用Yahoo的公开API做天气预报
- 【转】 使用Yahoo的公开API做天气预报
- 利用axis2调用远程WEB服务,获取天气预报
- 使用 XSLT、KML 和 Google Maps API 在地图上覆盖数据,第 1 部分:利用 Google Geocoder Web 服务
- 使用jQuery、Yahoo API和HTML5的geolocation来开发一个天气预报web应用
- 使用Yahoo的公开API做天气预报
- 使用jQuery、Yahoo API和HTML5的geolocation来开发一个天气预报web应用
- 如何利用Google的Web服务(GOOGLE API)
- Java学习之利用yahoo weather api 获取天气预报
- 用HTML5、地理定位API和Web服务来开发移动应用
- 利用HTML5 Web Audio API给网页JS交互增加声音
- 使用Python WSGI编写Web服务器前端,并利用Boost.Python在同一进程内连接到C++服务后端实现
- 怎样利用CSDN论坛公开的API实现自己的论坛工具
- 利用Python创建一个简单web服务
- 提高生产力:发送邮件API和Web服务(包含源码)
- 利用.Text提供的Web服务接口进行开发出现的问题。
- AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务-(个人拙笔)(转)
- 利用天气预报接口api提供天气服务