建一个文件,WeatherService.mxml,内容如下
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" initialize="init()">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
private function init():void{
myService.getWeatherbyCityName(ContactsConst.CURRENT_CITY_NAME);
}
private function getWeatherResult(event:ResultEvent):void{
var arr:ArrayCollection=ArrayCollection(event.result);//结果是一个包含字符串的集合
cityName.text=String(arr[1]);//第二个字符串是城市名称
todayWeather.text=String(arr[6]);//第六个是 当天日期和天气 类似这样2月20日 晴转多云
todayTemp.text=String(arr[5]);//第五个是 温度
tomoWeather.text=String(arr[13]);
tomoTemp.text=String(arr[12]);
afterWeather.text=String(arr[18]);
afterTemp.text=String(arr[17]);
}
private function getFault(event:FaultEvent):void{
Alert.show("网络连接错误!"+event.fault.message,"提示");
}
]]>
</mx:Script>
<mx:WebService id="myService" showBusyCursor="true" wsdl="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl" useProxy="false">
<mx:operation name="getWeatherbyCityName" result="getWeatherResult(event)" fault="getFault(event)"/>
</mx:WebService>
<mx:HBox width="100%" horizontalAlign="center">
<mx:Label id="cityName" fontSize="13"/>
<mx:Label id="todayWeather" fontSize="13"/>
<mx:Label id="todayTemp" fontSize="13"/>
<mx:Label id="tomoWeather" fontSize="13"/>
<mx:Label id="tomoTemp" fontSize="13"/>
<mx:Label id="afterWeather" fontSize="13"/>
<mx:Label id="afterTemp" fontSize="13"/>
</mx:HBox>
</mx:VBox>