您的位置:首页 > 其它

Web Service学习笔记:利用YAHOO公开API做天气预报Web服务

2008-10-14 21:34 627 查看
难道是因为在这个地方,我放了个本人在我的小站的链接地址,就违反了首页的规则,这绝对的原创,我星星苦苦写的,虽说没有很高的技术含量,但也是有点的,在放一次,如果被撤下来,希望能给我个合理的解释。在此,谢谢园子里了的管理人员。

学了一段时间的Web服务,今天做了一个Web服务,利用YAHOO的公开天气API做自己的Web服务,主要是想练练手。现在把过程和心得分享给大家。

文章在我小站的地址:Web Service学习笔记:利用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://weather.yahoo.com/forecast/CHXX0259_f.html</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://weather.yahoo.com/forecast/CHXX0259_f.html</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="

"/><br />

<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

别忘了"using System.Xml;"哦!

Web服务的代码中有一个Web公开方法,四个私有方法:

(1)GetWeather方法是公共方法,提供Web调用。

(2)FToC方法,他主要是把RSS返回的华氏温度转换成摄氏温度,其实这一步可以不用的,当初没发现,URL中加点参数就返回的是摄氏温度。

(3)EweekToCweek方法,他主要是把英文的星期缩写变成中文。

(4)EmonthToCmonth方法,它主要是把英文的月份缩写变成中文,并重新排序。

(5)CityNameToCityNum方法,这个最重要,他是把省会和直辖市的名字转换为编号,因为YAHOO传的参数不是城市名字的区号,全是自己的,而我又想不到更好的获得YAHOO城市对应的编号的方法,所以就只能支持这么几个城市了,希望有高手提出更好的方法,能不用这样,直接找YAHOO获取编号。

<?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>

写完了!最后希望能给大家带来收获!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: