使用 Web 服务捕获和处理 FlexMobile 项目中的 json 解析错误
我有一个 FlexMobile 项目,它从 http Web 服务检索购物车数组。如果购物车中没有商品,则返回的 json 为空,从而导致解析错误
这是 JSON 响应:
{ "cart": [ ] }
这是错误:
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()
这是我正在使用的代码。这是我第一次尝试 Flex 应用程序,所以期待新手方法等......
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
空数组是 Flex JSON 序列化器解码器的一个已知问题。估计会在flex4.5中修复。请参阅以下链接:
http://forums.adobe.com/thread/605070
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