android-如何处理gson错误

android-如何处理gson错误,android,Android,在我的应用程序中,如果单击该按钮,它将调用web服务以获取响应,在映射时使用GSON将该响应映射到某个类之后,我将得到以下错误: com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@44c51f20 failed to deserialized json object 如何处理它。有人能帮我吗 我的代码 private

在我的应用程序中,如果单击该按钮,它将调用web服务以获取响应,在映射时使用GSON将该响应映射到某个类之后,我将得到以下错误:

com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@44c51f20 failed to deserialized json object
如何处理它。有人能帮我吗

我的代码
private OnClickListener exit3Listener=new OnClickListener(){
公共void onClick(视图v){
如果(!exit3status){
System.out.println(“exit3可见”);
//System.out.println(“size”+RB_Constant.upcomingexits_obj.response.size());
//呼叫出口餐厅的Web服务。
Exitexpand_pd=ProgressDialog.show(RB_UpcomingExits.this,“,”请稍候…”,true);
线程t=新线程(){
公开募捐{
试试{
setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u NOSENSOR);
ExitRestaurantResponse=getExitsRestaurants();
System.out.println(“exit3response”+ExitResponse);
}捕获(SOAPE){
e、 printStackTrace();
}                   
rb_exitexand_Handler.post(rb_exitrestaurantresp);
退出并解散();
}                                               
};
t、 start();
listLayout3.setVisibility(View.VISIBLE);
exit3status=true;
如果(!exit1status | | exit2status | | exit4status | exit5status)
{
//System.out.println(“exit2消失”);
listLayout1.setVisibility(View.GONE);
listLayout2.setVisibility(View.GONE);
listLayout4.setVisibility(View.GONE);
listLayout5.setVisibility(View.GONE);
exit1status=false;
exit2status=false;
exit4status=false;
exit5status=false;
}
}否则{
System.out.println(“exit3消失”);
listLayout3.setVisibility(View.GONE);
exit3status=false;
}
//LLExitDetails.invalidate();
}
};
最终可运行rb\u Exitrestaurantresp\u HandlerResp=新可运行(){
公开募捐{
试一试{
设置请求方向(活动信息屏幕方向传感器);
检查_exitrestrepWSResponse();
}捕获(例外e){
e、 printStackTrace();
}
}
};
public void check_exitrestrepWSResponse()引发异常{
试一试{
com.google.gson.gson gson=new com.google.gson.gson();//或使用new GsonBuilder().create();
RB_Constant.exitsrestaurants_obj=gson.fromJson(ExitResponse,exitsrestaurants.class);//将json反序列化为target2
如果(RB_常量.exitsu_对象状态)等于(“失败”)!=true&&
RB_常量.exitsrestaurants_对象状态.equals(“成功”)==true)
{
//System.out.println(“总大小:+RB_常量。exitsrestaurants_对象totalrecords”);
System.out.println(“res size:+RB_Constant.exitsResponse.size());
if(exitRestaturantResponse.contains(“超时已过期”)!=true&&
ExitResponse.contains(“无法打开数据库”)!=true&&
ExitRestaturantResponse.contains(“错误”)!=true)
{
//出口大小是否大于零
if(RB_常量.exitsObj.response.size()>0){
if(RB_常量.exitsu_对象响应.size()>2){
if(RB_Constant.exitsrestaurants_obj.response.get(0).listRestaurants.size()>0){
System.out.println(“Exit3餐厅”);
//动态创建视图,而不是使用ListView
rbupcadapter3=新的UpcomingResultsListViewAdapter3(此);
int numItems3=0;
if(RB_Constant.exitsrestaurants_obj.response.get(0.listRestaurants.size()>0)
{
numItems3=RB_Constant.exitsrestaurants_obj.response.get(0.listRestaurants.size();
System.out.println(“Exit3餐厅大小:+numItems3”);
}
//线性布局2
对于(int位置=0;位置
通过使用try-catch块

try{
  // Try your GSON thing
} catch (JsonParseException e){
  // Do your Error handling
}

通过使用try-catch块

try{
  // Try your GSON thing
} catch (JsonParseException e){
  // Do your Error handling
}

我只是这样做,但不起作用。为什么会出现这种异常?您在代码中使用两行GSON解析器,并发布114行而不是这两行?一个人能有多懒。。。另外,我告诉过您发布和示例输入(您试图解析的JSON)。此外,还需要使用
Exitsrestaurants
-类来查看JSON是否无效。我使用的api不稳定,有时会发回html字符串而不是JSON。在本例中捕获JsonParseException有助于防止在格式错误的json arr时应用程序崩溃