1 Reply Latest reply on Dec 12, 2007 5:30 PM by 2JZ

    HTTPService and ArrayCollection Error

    magnatronus
      Update:

      After some debugging I think I have found my own answer .

      I have added the following line to the event code:

      var i:int = evt.result.result.item.length;

      When debugging this value if my XMl list has 2 or more items then the length value returns 2 or more.

      i.e. 2 items length =2, 6 items length=6 and so on.

      but for 1 item the length =0 (?) so although there is 1 item in the list and it shows in the debugger the value reported is 0 which might explain wht it cannot be set as an ArrayCollection.
      ------------------------------------------------------------------------------------------ ------------------------------------------------------

      I am processing the result of an HTTPService send in an event to create an arraycollection of items( using some example code I found on the net). Basically if the data is received OK then the items in the XML are cast an an arraycollection and they are processed. The lines to cast this is as follows.

      var source:ArrayCollection = evt.result.result.item as ArrayCollection;
      var cursor:IViewCursor = source.createCursor();
      while (!cursor.afterLast){
      var currentObj:Object = cursor.current;
      //..... code here to manipulate data
      cursor.moveNext();
      }

      I then iterate through the cursor. The problem I have is that it all works fine if there are 2 or more "items" returned in the XML but if there is only 1 item the source var does not get created and so the rest of the code fails. Does anyone have any ideas as to why ? I guess I can "get around it" by detecting only 1 item and processing it differently but it would be good to use the same code.

      btw. only be using flex for 4 days so I think it is very probably my extreme lack of actionscript knowledge.

      thanks.
        • 1. Re: HTTPService and ArrayCollection Error
          2JZ Level 1
          I had the same problem and I got the solution from the net:

          if( event.result.dataroot.city is ArrayCollection )
          myAC = event.result.dataroot.city as ArrayCollection
          else
          myAC = new ArrayCollection(ArrayUtil.toArray(event.result.dataroot.city));

          I don't mean to hijack the post but it seems relevant. I encounter the same problem with the Array class (instead of ArrayCollection). I've used the above method and it does return the length of 1. However, I tried to access the sub-element and it crashes because that becomes a null.

          Again, the array was set as follow initially which crashes right the way when there is one market on that day:
          MarketArray = event.result.list.day.Market.source as Array;

          This will be fine:
          MarketArray = new Array (ArrayUtil.toArray(event.result.list.day.Market.source ));
          But, as soon as I access the sub-items underthe market, it return null.
          var iSize = MarketArray[0].stats.length; // where it should return 3 items.

          Anyone has input?