Search code examples
apache-flexflex3httpservice

HTTPService Result - checking number of items with a specified name


I have a question about HTTPService and the data it returns.

Well lets consider this XML:

<PhotoGalleryData>
    <Photo>
        <id>1</id>
        <name>Summer Vacation</name>
        <description>In vacation</description>
        <fullImage>originalImg/1.JPG</fullImage>
    </Photo>
    <Photo>
        <id>2</id>
        <name>Winter Vacation</name>
        <description>coold</description>
        <fullImage>originalImg/2.JPG</fullImage>
    </Photo>
</PhotoGalleryData>

As you see i have two instances of Photo, that would be retrieved using a HTTPService, well then on the Result Event of that same HTTPService i would want him the count the amount of instances named Photo he as returned on is .lastResult.

This is a dumb question, but i can't find it anywhere in Adobe Docs.

Of course any help, hint, suggestion is greatly appreciated.


Medoix

I gotta be blind or something, because it still returns 0.

Something missing here?

MXML

<mx:HTTPService id="getData"
    url="{XMLDataFileLocation}"
    showBusyCursor="true"
    fault="getDataFaultHandler()"
    result="getDataResultHandler(event)"/>

ActionScript

import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
private var xmlData:XMLList;
private var numItems:int;
private function getDataResultHandler(evt:ResultEvent):void
{
    if (evt.result.PhotoGalleryData)
    {
        xmlData = XML(evt.result).descendants("Photo");
        numItems = xmlData.length();
        Alert.show('Nº '+numItems,'num de Photo');
    }
}

Solution

  • in the http_result function you have you will be putting this data in an XMLList for an example and then you can call the xmllist.length();

    private var xmlData:XMLList;
    private var numItems:Integer;
    
    private function HttpResult(evt:ResultEvent):void {
        if (evt.result.PhotoGalleryData) {
            xmlData = XML(evt.result).descendants("Photo");
            numItems = xmlData.length();
        }
    }
    

    EDIT: Do the below...

    Change

    <mx:HTTPService id="getData"
        url="{XMLDataFileLocation}"
        showBusyCursor="true"
        fault="getDataFaultHandler()"
        result="getDataResultHandler(event)"/>
    

    To...

    <mx:HTTPService id="getData"
        url="{XMLDataFileLocation}"
        resultFormat="e4x";
        showBusyCursor="true"
        fault="getDataFaultHandler()"
        result="getDataResultHandler(event)"/>
    

    This is working for me.