HTTPService问题将GeoCode结果返回给DataGrid

时间:2011-01-05 04:27:08

标签: actionscript-3 flex flex3

我正在创建一个Flex3应用,以便从Google Geocoding API返回一些结果。

我使用TourDeFlex中的HTTPService Events示例作为模仿的参考指南。

目前,我已经硬编码了Google的回复以使用此XML响应: maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

网络监视器建议我从XML获得有效的响应,但我想我没有正确引用它来填充我的DataGrid?

我已经筛选了问题here

以下是我的代码的精简版:

private function resultHandler(event:ResultEvent):void
        {
        results = event.result.GeocodeResponse.result;
        }

        private function faultHandler(event:FaultEvent):void
        {
        Alert.show(event.fault.faultDetail, "Error");
        }           

    ]]>
</mx:Script>

<mx:HTTPService id="srv" 
            url="http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&amp;sensor=false"
        result="resultHandler(event)"
            fault="faultHandler(event)"/>

<mx:TitleWindow title="Find" showCloseButton="true" close="closeMe();"
            styleName="formPanel" horizontalScrollPolicy="off" verticalScrollPolicy="off"
            width="400" height="200">

    <mx:DataGrid dataProvider="{results}" width="100%" height="100%">
        <mx:columns>
        <mx:DataGridColumn dataField="type" headerText="Address"/>
        </mx:columns>
    </mx:DataGrid>

  <mx:Button label="Go" height="20" styleName="buttonGo" click="srv.send()"/>
</mx:TitleWindow>

1 个答案:

答案 0 :(得分:2)

看看xml。可能是类型的第一次出现处于顶层,但从那时起类型 address_component 的元素。

也许尝试 address_component.type 作为DataGridColumn的数据字段?或者将dataprovider设置为results.address_component?

修改更新

因为它读取XML,可能在resultFormat="e4x"标记内设置<HttpService ..>,并且有一个等待结果的XMLListCollection:

<mx:XMLListCollection id="xc" source="{srv.lastResult.result}"/>

<mx:DataGrid id="dg" dataProvider="{xc}">

在调试模式下,在XC上设置监视以确保其被填充