代码之家  ›  专栏  ›  技术社区  ›  Fábio Antunes

用httpService填充arraycollection

  •  0
  • Fábio Antunes  · 技术社区  · 15 年前

    是的,有这样一个问题,但没有活动,也没有答案。

    我希望使用httpservice从外部XML文件加载数据,并且在同一httpservice的resultEvent上,我希望他用XML中的数据填充arraycollection。

    我认为ArrayCollection是这个XML的理想选择。但我愿意接受建议。

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <PhotoGalleryData>
        <Photo>
            <id>1</id>
            <name>Summer Vacation</name>
            <description>In vacation</description>
            <source>vacation.JPG</source>
        </Photo>
        <Photo>
            <id>2</id>
            <name>Winter Vacation</name>
            <description>coold</description>
            <source>vacation2.JPG</source>
        </Photo>
    </PhotoGalleryData>
    

    我以为这条简单的线 getDataResultHandler() ,将足以填充ArrayCollection。

    <mx:HTTPService id="getData"
            url="{XMLDataFileLocation}"
            fault="getDataFaultHandler()"
            result="getDataResultHandler()"/>
    
    [Bindable]
    private var PhotoData:ArrayCollection;
    
    private function getDataResultHandler():void
    {
        PhotoData = new ArrayCollection(getData.lastResult.PhotoGalleryData.photo)
    }
    

    但我想不是,因为我只是确定我已经放置了一个绑定到arraycollection的列表,以查看它是否真的被填充了。

    <mx:List dataProvider="{PhotoData}" labelField="name"/>
    

    列表中没有显示任何数据,因此无法正常工作。

    谢谢你的帮助。

    编辑

    注释

    这个 <mx:List/> 过去只是为了 确定arraycollection是 实际上,它不会用于 应用程序。


    接受博日建议的结果。

    使用bozho changes,flex不再报告var类型错误,但在我运行它之后。Adobe Flash确实报告了这一点。

    类型错误:错误1034:类型强制 失败:无法转换 mx.utils::objectProxy@22CD311收件人 mx.collections.arraycollection。在 photogallery/getdataresulthandler()[c:\users\f_bio 安图内斯\文档\ flex builder 3 \照片 库\SRC\ActionScripts\PhotoGallery.as:56] 在 照片库/uu getdata_result()[c:\users\f_bio 安图内斯\文档\ flex builder 3 \照片 Gallery\src\photogallery.mxml:23]在 flash.events::EventDispatcher/DispatcheventFunction()。 在 flash.events::EventDispatcher/Dispatchevent()。 在 mx.rpc.http.mxml::httpService/ http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[C :\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\http\mxml\httpservice.as:290] 在 rpc::abstractInvoker(抽象调用程序)/ http://www.adobe.com/2006/flex/mx/internal::resultHandler()[C :\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\abstractinvoker.as:193] 在 mx.rpc::responder/result()[c:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\responder.as:43] 在 mx.rpc::asyncRequest/acknowledge()[c:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\asyncRequest.as:74] 在 directhttpmessageresponder/completehandler()[c:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\directhttpchannel.as:403] 在 flash.events::EventDispatcher/DispatcheventFunction()。

    嗯,第23行的flash报告了错误,它是:

    PhotoData = ArrayCollection(event.result);
    

    第23行是:

    result="getDataResultHandler(event)"
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Ryan Lynch    15 年前

    如果您可以使用 XMLListCollection 代替一个 ArrayCollection 转换结果对象的过程更简单。 Here 这是一个很好的教程,可以解释如何进行。

    编辑:

    本教程的关键内容如下:

    • 您需要将服务的结果格式设置为 e4x .
    • 您需要将结果对象强制转换为 XML 对象,将重复节点提取为 XMLList ,并构造一个 XmlListCollection 从这样的列表中:

      private function httpService_result(evt:ResultEvent):void 
      {
           var xmlList:XMLList = XML(evt.result).path.to.repeating.element;
           xmlListColl = new XMLListCollection(xmlList);
      }
      
        2
  •  0
  •   Ben Johnson    15 年前

    您可以这样简化脚本:

    <mx:HTTPService id="getData" url="{XMLDataFileLocation}"/>
    <mx:List dataProvider="{getData.lastResult.Photo}" labelField="name"/>
    

    这个 lastResult 你的 getData 将是XML的根。通过检索 lastResult.Photo 你会得到一个 XMLList 照片中的