flex 和webService 通讯

来源:互联网 发布:ios软件安装目录 编辑:程序博客网 时间:2024/04/29 15:03

在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>

原创粉丝点击