2013-03-15 33 views

回答

0

這取決於您的Web服務和它提供的數據類型。 許多Web服務可以返回一個響應作爲對象列表。 其他人給你一個結構化的字符串。

在我的示例中,我使用了一個簡單的公共天氣Web服務,它將XML作爲字符串返回。

這是一個服務描述:http://www.webservicex.com/globalweather.asmx?wsdl 這是一個測試頁面:http://www.webservicex.com/globalweather.asmx?test

我用GetCitiesByCountry方法來獲得這個名單: enter image description here

//源代碼

<?xml version="1.0" encoding="utf-8"?> 
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
      xmlns:s="library://ns.adobe.com/flex/spark" 
      xmlns:mx="library://ns.adobe.com/flex/mx" 
      minWidth="955" minHeight="600" creationComplete="init()"> 
<fx:Script> 
    <![CDATA[ 
     import mx.controls.Alert; 
     import mx.rpc.CallResponder; 
     import mx.rpc.events.FaultEvent; 
     import mx.rpc.events.ResultEvent; 
     import services.globalweather.Globalweather; 

     private var ws:Globalweather = new Globalweather(); 
     private var getCitiesByCountryCR:CallResponder; 

     private function init():void 
     { 
      getCitiesByCountryCR = new CallResponder(); 
      getCitiesByCountryCR.addEventListener(ResultEvent.RESULT, onResult); 
      getCitiesByCountryCR.addEventListener(FaultEvent.FAULT, onFault); 
     } 

     private function onResult(evt:ResultEvent):void 
     { 
      var xml:XML = new XML(evt.result); 
      var xmlList:XMLList = xml.Table.City; 

      taCities.text = ""; 
      for each (var item:XML in xmlList) 
      { 
       taCities.text += item.toString() + String.fromCharCode(13); 
      } 
     } 

     private function onFault(evt:FaultEvent):void 
     { 
      Alert.show("Fault!"); 
     } 

     protected function getCities(event:MouseEvent):void 
     { 
      getCitiesByCountryCR.token = ws.GetCitiesByCountry(tiCountry.text); 
     } 

    ]]> 
</fx:Script> 

<s:VGroup x="20" y="20" width="330" height="200"> 
    <s:HGroup verticalAlign="bottom"> 
     <s:Label text="Enter country name:"/> 
     <s:TextInput id="tiCountry" text="France"/> 
     <s:Button x="202" y="10" label="Get cities!" click="getCities(event)"/> 
    </s:HGroup> 

    <s:TextArea id="taCities" height="100%" width="100%"/> 
</s:VGroup> 

</s:Application> 
相關問題