I am learning Flex 2 for a project I'm working on with a friend. We decided, for simplicity's sake, to have our web service connection to the back-end be relatively simple to begin with: Just use HTTP POST. But we ran into a snag, which, in doing a search on Google, I discovered that many others have run into.
When you use a web service to pull a list of XML nodes into Adobe Flex, what happens is that Flex generates a "result" object that contains the xml you're trying to read in. As the Flex developer, you're expected to declare an object of type ArrayCollection, set it equal to the ArrayCollection in the Result collection, and you're good to go. Frustratingly, that's where a lot of the documentation I found on this ends, and it's not the whole picture. The problem is with that ArrayCollection in the Result object that Flex creates for you: The problem is it does NOT always do it! It only does it in cases where it "has to," that is, for xml containing node lists with a count of 2 or more.
- For node lists of two or more nodes, Flex recognizes that it "needs" an ArrayCollection to contain them, and creates the ArrayCollection in the Result object to contain them.
- For node lists of exactly one node, Flex recognizes that it "needs" to only create a single-object container, and chooses to do this by creating an object of type ObjectProxy in the Result object to contain it. For consistency in your application, I would recommend that you use Flex' toArray() function to convert the ObjectProxy to ArrayCollection.
- For node lists of exactly zero nodes, Flex decides, rightly so, to set its internal representation of the node collection to NULL. In that case, for consistency, I would recommend that you simply create an empty ArrayCollection.
Sample code is below. I've included the web service "mx:HTTPService" definition and its call to the parsing function.
private var reminderList:ArrayCollection;
private function ParseReminderList(event:ResultEvent):void
if (event.result.Reminders == null)
else if (event.result.Reminders.Reminder is ArrayCollection)
else if (event.result.Reminders.Reminder is ObjectProxy)
reminderList = new ArrayCollection(ArrayUtil.toArray(event.result.Reminders.Reminder));
<mx:HTTPService id="ReminderListService" url="http://localhost/reminders.asp" result="ParseReminderList(event)"/>