flex 和webService 通讯

在flex中和webservice是很简单,使用Webservice类就好了。

先说一下他的使用呀

   web=new WebService();

//这是一个webservice的地址;
    web.wsdl = "http://www.webservicex.net/globalweather.asmx?wsdl";   

   web.loadWSDL();

这样就已经连接上了.

想监控连接是否成功可以注册LoadEvent.LOAD事件

   web.addEventListener(LoadEvent.LOAD,OnLoad);

下来就可以调用webservice的接口了,激动的时刻来了,如果接口有返回值的话,要先监听一下结果

 

   web.GetCitiesByCountry.addEventListener(ResultEvent.RESULT, OnGetCity);
   web.GetCitiesByCountry("china");

  private function OnGetCity(event:ResultEvent ):void{
   var xml:XML=new XML(event.result);

  }

 

这样就可以了,很简单吧.

 

 

下面给大家一个例子,天气预报的

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">
<mx:Script>
 <![CDATA[
  import mx.rpc.soap.LoadEvent;
  import mx.rpc.soap.WebService;
        import  mx.rpc.events.ResultEvent;
  [Bindable]
  private var city:Array=new Array();
  private var web:WebService;
  private function init():void{
   web=new WebService();
            web.wsdl = "http://www.webservicex.net/globalweather.asmx?wsdl";   

   web.addEventListener(LoadEvent.LOAD,OnLoad);
   web.loadWSDL();
   web.GetCitiesByCountry.addEventListener(ResultEvent.RESULT, OnGetCity);
   web.GetCitiesByCountry("china");
   web.GetWeather.addEventListener(ResultEvent.RESULT,OnGetWeather);
  }
  
  private function OnLoad(event:Event):void{
   pnlWeather.title+="------ webservice connected ... " 
  }
  
  private function OnGetCity(event:ResultEvent ):void{
   var xml:XML=new XML(event.result);
   for each ( var name:XML in xml..City ) {
    city.push(name);
   }  
   cmbCity.selectedIndex=0;


  }
  
  private function OnGetWeather(event:ResultEvent ):void{
   txtRWeather.text="";
   var xml:XML=new XML(event.result);
   var xmlList:XMLList=xml.children();
   for (var i:int =0;i<xmlList.length();i++){
    
    txtRWeather.text+= xmlList[i].toXMLString()+"/n";
   }
  }
  
  private function OnClick():void{
    web.GetWeather(cmbCity.selectedLabel,"china");  
  }
 ]]>
</mx:Script>
<mx:Panel title="Weather"  height="262" width="421" layout="absolute" id="pnlWeather">
 <mx:Label text="city:" x="74" y="24"/>
 <mx:ComboBox x="130" y="24" width="106" id="cmbCity" dataProvider="{city}"></mx:ComboBox>
 <mx:Button x="258" y="24" label="提交" click="OnClick()"/>
 <mx:TextArea x="130" y="54" width="261" height="156" id="txtRWeather"/>
 <mx:Label x="74" y="53" text="weather"/>

 

</mx:Panel>
</mx:Application>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值