Pergunta

I have this FlexMobile Project that retrieves a cart array from a http web service. If the cart has no items in it, the json returned is empty, thus causing a parse error

Here is the JSON response:

{ "cart": [ ] }

Here is the error:

Error: Error while Parsing
at com.adobe.serializers.json::JSONDecoder/parseValue()[C:\opt\sb\ide_trunk\ide_builder\com.adobe.flexbuilder.dcrad\serializers\src\com\adobe\serializers\json\JSONDecoder.as:249]
at com.adobe.serializers.json::JSONDecoder/parseArray()[C:\opt\sb\ide_trunk\ide_builder\com.adobe.flexbuilder.dcrad\serializers\src\com\adobe\serializers\json\JSONDecoder.as:431]
at com.adobe.serializers.json::JSONDecoder/parseValue()[C:\opt\sb\ide_trunk\ide_builder\com.adobe.flexbuilder.dcrad\serializers\src\com\adobe\serializers\json\JSONDecoder.as:222]
at com.adobe.serializers.json::JSONDecoder/parseObject()[C:\opt\sb\ide_trunk\ide_builder\com.adobe.flexbuilder.dcrad\serializers\src\com\adobe\serializers\json\JSONDecoder.as:339]
at com.adobe.serializers.json::JSONDecoder/parseValue()[C:\opt\sb\ide_trunk\ide_builder\com.adobe.flexbuilder.dcrad\serializers\src\com\adobe\serializers\json\JSONDecoder.as:217]
at com.adobe.serializers.json::JSONDecoder/decode()[C:\opt\sb\ide_trunk\ide_builder\com.adobe.flexbuilder.dcrad\serializers\src\com\adobe\serializers\json\JSONDecoder.as:178]
at com.adobe.serializers.json::JSONSerializationFilter/deserializeResult()[C:\opt\sb\ide_trunk\ide_builder\com.adobe.flexbuilder.dcrad\serializers\src\com\adobe\serializers\json\JSONSerializationFilter.as:78]
at mx.rpc.http::AbstractOperation/http://www.adobe.com/2006/flex/mx/internal::processResult()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\rpc\http\AbstractOperation.as:967]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:313]
at mx.rpc::Responder/result()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\rpc\Responder.as:56]
at mx.rpc::AsyncRequest/acknowledge()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:84]
at DirectHTTPMessageResponder/completeHandler()[E:\dev\hero_private_beta\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:451]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

Here is the code I am using. This is my first attempt at a flex application, so expect newbie methods etc...

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark"
    xmlns:rincomobile="services.rincomobile.*"
    title="view_cart">
<fx:Script>
    <![CDATA[
        import com.adobe.serializers.utility.TypeUtility;

        import mx.events.FlexEvent;
        import mx.rpc.events.FaultEvent;

        private function onFault( event : FaultEvent ) : void
        {
            trace(event.fault.message.toString());
        }

        protected function list_creationCompleteHandler(event:FlexEvent):void {
            getCartResult.token = rincoMobile.getCart("getCart");
            getCartTotalResult.token = rincoMobile.getCartTotal("getCartTotal");
        }

        protected function getCartTotal(fuseaction:String):void {
            getCartTotalResult.token = rincoMobile.getCartTotal(fuseaction);
        }

        protected function getCartResult_faultHandler(event:FaultEvent):void
        {
            // TODO Auto-generated method stub
            trace("something happened");
        }

    ]]>
</fx:Script>
<fx:Declarations>
    <s:CallResponder id="getCartResult" fault="onFault(event)" />
    <rincomobile:RincoMobile id="rincoMobile" />
    <s:CallResponder id="getCartTotalResult"/>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:List id="list" left="0" right="0" top="0" bottom="176" borderVisible="false"
        creationComplete="list_creationCompleteHandler(event)"
        itemRenderer="renderers.cartRenderer" labelField="item_code">
    <s:AsyncListView list="{TypeUtility.convertToCollection(getCartResult.lastResult.cart)}"/>
</s:List>
<s:Button id="btn_checkout" bottom="93" width="90%" label="${getCartTotalResult.lastResult.cart[0].total}  Checkout" chromeColor="#091A33" horizontalCenter="0"/>
<s:Button bottom="10" width="90%" label="Continue Shopping" chromeColor="#091A33"
          horizontalCenter="0"/>
</s:View>
Foi útil?

Solução

The empty array is a know issue with Flex JSON serializer decoder. Supposedly it will be fixed in flex4.5. See the following link: http://forums.adobe.com/thread/605070

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top