Flex List显示Web服务的键值对

时间:2013-03-15 03:08:14

标签: web-services flex wsdl flex4.5

如何通过从wsdl Web服务获取键值对来显示Flex中的数据列表?请帮忙。

1 个答案:

答案 0 :(得分: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>