利用YAHOO公開API做天氣預報Web服務

作者: VisualStudio  來源: 博客園  發布時間: 2008-10-14 23:48  閱讀: 36689 次  推薦: 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
 
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()