views:

195

answers:

2

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');
    }
}
+2  A: 

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.

medoix
Strange numItems returns the value of 0 instead of 2.
Fábio Antunes
Wouldn't be something like this? numItems = xmlData.child("Photo").length();
Fábio Antunes
sorry i missed something and have updated in code.
medoix
Sorry Medoix, i gotta be blind or something, it still returns 0, could you take a look at my code in my new edit.
Fábio Antunes
no you are not blind it is me not testing my code :P i have no updated my answer with a change to the httpService and i have TESTED it lol
medoix
SUCESS.. Haha Finally xD. My greatest thanks xD. I will be following your blog medoix.com :)
Fábio Antunes
A: 

Just do the following. it will solve your probs ;)

private var xmlData:XMLList;
private var numItems:Integer;

private function HttpResult(evt:ResultEvent):void {
    if (evt.result.PhotoGalleryData) {

        numItems = ArrayCollection(evt.result.PhotoGalleryData.Photo).length;

    }
}

RSTanvir

RSTanvir
Flex doesn't report any error, but Flash does: Error #1034: Type Coercion failed: cannot convert XMLList@2107551 to mx.collections.ArrayCollection.
Fábio Antunes