flex调用webservice(以天气预报为例)

建一个文件,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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值