雅虎API创建天气预报

yahoo天气预报的url是http://weather.yahooapis.com/forecastrss?w=2151330&u=c参数w对应各个地方在yahoo数据库中的WOEID,如北京的WOEID是2151330,目前也可以用参数p来代替w,不过官方还是推荐用w,p是计划要被取消的。参数u用于确定温度的单位(c是摄氏度f是华氏度)。对于中国(国外没比较)的天气查询来说,yahoo相对无论从能查到的地方还是预报的内容都是比较全的。

详细的API文档:http://developer.yahoo.com/weather/

请求xml

var url:String = "http://weather.yahooapis.com/forecastrss" + "?w=" + (location number) + "&u=" + ("c" 摄氏温度或者 "f" 华氏温度); 

 

获取地址编号

地址编号必须是一个WOEID,你可以通过雅虎的天气预报首页查询你需要的地址编号。这个编号在
你想要的那个城市天气预报页的URL中。你也可以通过在首页中输入你的邮政编码查询WOEID。例如,你想查询洛杉矶的天气情况,这个城市的天气预报页的URL是
http://weather.yahoo.com/united-states/california/los-angeles-2442047/,所以它的WOEID是2442047.

理解xml

  当你请求任何地方的天气情况时,你会接收到如下结构的xml:

<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 - Los Angeles, CA</title>  

        <link>http://us.rd.yahoo.com/dailynews/rss/weather/Los_Angeles__CA/*http://weather.yahoo.com/forecast/USCA0638_c.html</link>  

        <description>Yahoo! Weather for Los Angeles, CA</description>  

        <language>en-us</language>  

        <lastBuildDate>Mon, 01 Mar 2010 5:47 am PST</lastBuildDate>  

        <ttl>60</ttl>  

        <yweather:location city="Los Angeles" region="CA"   country="United States"/>  

        <yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/>  

        <yweather:wind chill="12"   direction="0"   speed="0" />  

        <yweather:atmosphere humidity="80"  visibility="16.09"  pressure="1018.4"  rising="1" />  

        <yweather:astronomy sunrise="6:22 am"   sunset="5:49 pm"/>  

        <image>  

            <title>Yahoo! Weather</title>  

            <width>142</width>  

            <height>18</height>  

            <link>http://weather.yahoo.com</link>  

            <url>http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>  

        </image>  

        <item>  

            <title>Conditions for Los Angeles, CA at 5:47 am PST</title>  

            <geo:lat>34.05</geo:lat>  

            <geo:long>-118.25</geo:long>  

<link>http://us.rd.yahoo.com/dailynews/rss/weather/Los_Angeles__CA/*http://weather.yahoo.com/forecast/USCA0638_c.html</link>  

            <pubDate>Mon, 01 Mar 2010 5:47 am PST</pubDate>  

            <yweather:condition  text="Fair"  code="33"  temp="12"  date="Mon, 01 Mar 2010 5:47 am PST" />  

            <description><![CDATA[> 

            <img src="http://activetuts.s3.amazonaws.com/tuts/093_weather/Tutorial/http://l.yimg.com/a/i/us/we/52/33.gif"/><br /> 

            <b>Current Conditions:</b><br /> 

            Fair, 12 C<BR /> 

            <BR /><b>Forecast:</b><BR /> 

            Mon - Mostly Cloudy. High: 20 Low: 10<br /> 

            Tue - AM Clouds/PM Sun. High: 19 Low: 9<br /> 

            <br /> 

            <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Los_Angeles__CA/*http://weather.yahoo.com/forecast/USCA0638_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> 

            (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/> 

            ]]></description>  

            <yweather:forecast day="Mon" date="1 Mar 2010" low="10" high="20" text="Mostly Cloudy" code="28" />  

            <yweather:forecast day="Tue" date="2 Mar 2010" low="9" high="19" text="AM Clouds/PM Sun" code="30" />  

            <guid isPermaLink="false">USCA0638_2010_03_01_5_47_PST</guid>  

        </item>  

    </channel>  

</rss><!-- api7.weather.re4.yahoo.com compressed/chunked Mon Mar  1 06:59:00 PST 2010 -->  

 

解析xml
现在你已经对这个xml有了更好的理解了,我们现在需要做的就是将这些数据指派给变量,然后用这些数据去创建我们的应用程序。
  因此我们需要创建一些变量来加载XML。下面是代码(把这些代码放在你的文档类的相关地方)。

//This is going to contain all the data from the XML  包含xml的所有数据

private var _xmlData:XML;  

//This is going to be the url of the XML that we will load  xml加载地址

private var _xmlURL:String;  

  

private function loadXML(xmlURL:String):void {  

    var loader:URLLoader = new URLLoader();  

    var request:URLRequest = new URLRequest(_xmlURL);  

  

    loader.load(request);  

    loader.addEventListener(Event.COMPLETE, loadData);  

}  

  

private function loadData(event:Event):void {  

    _xmlData = new XML(event.currentTarget.data);  

  

    var yweather:Namespace = new Namespace("http://xml.weather.yahoo.com/ns/rss/1.0");  

    var day:String = _xmlData.channel.item.yweather::forecast[0].@day;  

    var codeToday:String = _xmlData.channel.item.yweather::forecast[0].@code;  

    var codeTomorrow:String = _xmlData.channel.item.yweather::forecast[1].@code;  

}  

 

你需要把_xmlData定义到所有方法外面(即定义成成员变量),因为你在代码的任何地方都用到它,不仅仅在一个方法中。

第一个方法loadXML(),加载xml文件到Flash中;我们使用事件监听来检查xml是否加载完成,在加载完成后运行 loadData()。

loadData()方法将接收到的xml数据保存在_xmlData变量中。我们使用namespace因为雅虎决定创建他们自己的XML(你可以在livedocs.adobe.com找到更多关于命名空间的信息)。
其他的变量用于在程序中显示信息。
(想知道更多关于AS3解析xml的信息,请看Dru Kepple的AS3:101 – XML 自学材料。)
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值