IDE:Flex builder 3
打開flex 2 language reference,可以看到web service 應用相關的namespace有兩個:
- mx.rpc.soap
- mx.rpc.soap.mxml
- <mx:WebService id="weatherWS" wsdl="http://www.webservicex.net/globalweather.asmx?WSDL" showBusyCursor="true" fault="wsFaultPopAlert()"/>
- weatherWS.GetCitiesByCountry("China");
聲明方式也是可以的:
- <mx:WebService id="weatherWS" wsdl="http://www.webservicex.net/globalweather.asmx?WSDL" showBusyCursor="true" fault="wsFaultPopAlert()">
- <mx:operation name="GetCitiesByCountry"/>
- mx:WebService>
- weatherWS.GetCitiesByCountry.send("china")
除此以外WebService提供兩個event:
分別代表調用出錯和成功事件,兩者都沒有提供用戶默認實現,需要用編程方式實現事件處理。特別是用ResultEvent可以獲得調用結果(就是本例開始獲得的xml結果,剩下的處理就是分析xml結構啦)。
下面是模塊代碼:
- xml version="1.0" encoding="utf-8"?>
- <mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="174" height="200" title="Weather" creationComplete="init()">
- <mx:Script>
- private var citysArr:Array = new Array();
- private function init():void
- {
- citysArr.push({label:"choose..."});
- cityCombo.selectedIndex = 0;
- weatherWS.addEventListener(ResultEvent.RESULT, wsResult);
- weatherWS.GetCitiesByCountry("China");
- this.title = "Weather(connecting...)";
- }
- private function wsResult(eve:ResultEvent):void
- {
- this.title = "Weather";
- weatherWS.removeEventListener(ResultEvent.RESULT,wsResult);
- XML.ignoreWhitespace = true;
- XML.ignoreComments = true;
- XML.ignoreProcessingInstructions = true;
- var xml:XML = new XML(eve.result.toString());
- var cityNum:int = xml.Table.length();
- for(var i:int=0 ; i
- {
- citysArr.push({label:xml.Table[i].City});
- }
- cityCombo.addEventListener(ListEvent.CHANGE, cityListChooseHandler);
- weatherWS.addEventListener(ResultEvent.RESULT, onCityInfo);
- }
- private function cityListChooseHandler(eve:ListEvent):void
- {
- var cityName:String = cityCombo.selectedLabel;
- cityInfo.htmlText = "";
- weatherWS.GetWeather(cityName,"china");
- }
- private function onCityInfo(eve:ResultEvent):void
- {
- var xml:XML = new XML(eve.result.toString());
- var infoNum:int = xml.children().length();
- for(var i:int=0 ; i
- {
- var nodestr:String = xml.children()[i].toXMLString();
- cityInfo.htmlText += "
- }
- }
- private function wsFaultPopAlert():void
- {
- Alert.show("WebService access failed");
- }
- ]]>
- mx:Script>
- <mx:WebService id="weatherWS" wsdl="http://www.webservicex.net/globalweather.asmx?WSDL" showBusyCursor="true" fault="wsFaultPopAlert()"/>
- <mx:Label x="0" y="0" text="City"/>
- <mx:ComboBox id="cityCombo" x="34" y="-2" width="120" dataProvider="{citysArr}" labelField="label"/>
- <mx:TextArea id="cityInfo" x="0" y="26" width="100%" height="100%" selectable="false"/>
- <mx:ControlBar>
- mx:ControlBar>
- mx:Panel>