您的位置:知识库 » .NET技术

利用YAHOO公开API做天气预报Web服务

作者: VisualStudio  来源: 博客园  发布时间: 2008-10-14 23:48  阅读: 37219 次  推荐: 1   原文链接   [收藏]  

系列文章导航:

创建一个示例和WebMethod特性解析

WebService特性和数组类型解析

类和结构体解析

利用YAHOO公开API做天气预报Web服务

Webservice 的设计和模式

Remoting和Webservice的区别


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

求教:这个Web服务还有个不完善的地方,Web服务的CityNameToCityNum方法,这个最重要,他是把省会和直辖市的名字转换为编号,因为YAHOO传的参数不是城市名字的区号,全是自己的,而我又想不到更好的获得YAHOO城市对应的编号的方法,所以就创建了HASHTABLE存储了中国的各个省会城市和直辖市,希望有高手提出更好的方法,能不用这样,直接找YAHOO获取编号,提取更多的城市,而不用把所有的中国所有的城市全写在HASHTABLE里。


Web服务地址:http://www.h2bbs.com/Weather/Weather.asmx

 

原理:

 在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/*[url]http://weather.yahoo.com/forecast/CHXX0259_f.html[/url]</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/*[url]http://weather.yahoo.com/forecast/CHXX0259_f.html[/url]</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="
[img]http://l.yimg.com/us.yimg.com/i/us/we/52/28.gif[/img]"/><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
 -->

 

1
0

.NET技术热门文章

    .NET技术最新文章

      最新新闻

        热门新闻