2013-03-15 30 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> 
相关问题