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