5 Replies Latest reply on May 10, 2010 7:28 PM by Sree_D

    Problems when XML HTTPservice returns a single item

    samslo

      my script.php returns this XML

       

      <all>

         <item>

            <field1>value1</field1>

            <field2>value2</field2>

         </item>

         <item>

            <field1>value1</field1>

            <field2>value2</field2>

         </item>

      </all>

       

      I'm using an HTTPService and have not specified a resultFormat so it's probably using the default resultFormat="object"

       

      I then bind to a List

       

      dataProvider="{getDataHTTP.lastResult.all.item}"

       

      I get no problems whe the number of items returned is more than 1, but when it's only 1 item I get a runtime error

       

      cannot convert XMLList to mx.collections.IList

       

      I tried different solutions such as casting the dataProvider to XMLListCollection, but it's not working or I'm not doing it right.

       

      Any idea how to solve this?

       

      Thanks

        • 1. Re: Problems when XML HTTPservice returns a single item
          Sree_D Level 2

          The result from HTTPService will be of type ObjectProxy but not complete object and the properties in side it (all.item in this case) will be treated as plain object but not as type ArrayCollection hence there will be problem if we try to cast it to a property of ArrayCollection or some datasource which expects a list or ArrayCollection.

           

          This can be handled by checking for object type and then adding it to a collection if it is single item. Hopw this helps.

          1 person found this helpful
          • 2. Re: Problems when XML HTTPservice returns a single item
            samslo Level 1

            Hi Sree, Thanks for the reply. I tried checking for the type, but in alaways says the type is ObjectProxy (when xml output is a single item and when xml output is multiple items)

             

            I'm doing it this way

             

                protected function getData_ResultHandler(event:ResultEvent):void{


                            if (event.result is ObjectProxy) {
                                Alert.show("object proxy");
                            }


                            if (event.result is ArrayCollection){
                                Alert.show("arraycollection");

                            }
                 }

             

            It always outputs "object proxy". Is my way of doing it incorrect?

            • 3. Re: Problems when XML HTTPservice returns a single item
              Flex harUI Adobe Employee

              Try resultFormat="e4x"

              • 4. Re: Problems when XML HTTPservice returns a single item
                samslo Level 1

                @Flex harUI

                 

                Re resultFormat="e4x", it was one of the things I tried but no luck.

                • 5. Re: Problems when XML HTTPservice returns a single item
                  Sree_D Level 2

                  Try to check something like this

                   

                  if(getVarType(ObjectProxy[propertyName],false) == "mx.collections::ArrayCollection")

                   

                  propertyName (item in your case)  is the property in ObjectProxy (event.result) which you want to check if single item or multiple items.

                   

                   

                    public static function getVarType(value:*, alert:Boolean = false):String
                    {
                     import flash.utils.*;
                     var description:XML = describeType(value);
                     if(alert)
                      Alert.show(description[0].@name);
                     return description[0].@name;
                    }

                  1 person found this helpful