开发者

android - How to handle gson error

In my application if you click on the button then it calls the web service for the response and after mapping this response to some class using GSON at the time of mapping I'm getting the following error:

com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@44c51f20 failed to deserialized json object

How to handle it. can anybody help me

My Code

private OnClickListener exit3Listener = new OnClickListener(){
public void onClick(View v){
    if(!exit3status){   
        System.out.println("exit3 visible");
        //System.out.println("size"+RB_Constant.upcomingexits_obj.response.size());
        // Web service calling for exit restaurants.
        Exitexpand_pd = ProgressDialog.show(RB_UpcomingExits.this, "", "Please wait...", true);
        Thread t = new Thread(){

            public void run(){
                try {               
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
                    ExitRestaurantsResponse = getExitsRestaurants();
                    System.out.println("exit3response"+ExitRestaurantsResponse);
                } catch (SoapFault e) {
                    e.printStackTrace();
                }                   
                rb_Exitexpand_Handler.post(rb_exitrestaurantres_HandlerResp);
                Exitex开发者_C百科pand_pd.dismiss();
            }                                               
        };
        t.start();  

        listLayout3.setVisibility(View.VISIBLE);
        exit3status = true;

        if(!exit1status || exit2status || exit4status || exit5status)
        {
            //System.out.println("exit2 GONE");
            listLayout1.setVisibility(View.GONE);
            listLayout2.setVisibility(View.GONE);
            listLayout4.setVisibility(View.GONE);
            listLayout5.setVisibility(View.GONE);

            exit1status = false;
            exit2status = false;
            exit4status = false;
            exit5status = false;
        }

    } else {
        System.out.println("exit3 GONE");
        listLayout3.setVisibility(View.GONE);   
        exit3status = false;                    
    }
    //LLExitDetails.invalidate();
}
};


final Runnable rb_exitrestaurantres_HandlerResp = new Runnable(){
    public void run(){
        try{
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
            check_exitrestrepWSResponse();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
};


public void check_exitrestrepWSResponse() throws Exception{
    try{
        com.google.gson.Gson gson = new com.google.gson.Gson(); // Or use new GsonBuilder().create();
        RB_Constant.exitsrestaurants_obj = gson.fromJson(ExitRestaurantsResponse, Exitsrestaurants.class); // deserializes json into target2
        if( RB_Constant.exitsrestaurants_obj.status.equals("FAILED") != true &&
            RB_Constant.exitsrestaurants_obj.status.equals("SUCCESS") == true)
        {
            //System.out.println("total size:"+RB_Constant.exitsrestaurants_obj.totalrecords);
            System.out.println("res size:"+RB_Constant.exitsrestaurants_obj.response.size());
            if(ExitRestaurantsResponse.contains("Timeout expired") != true &&
                ExitRestaurantsResponse.contains("Cannot open database") != true &&
                ExitRestaurantsResponse.contains("error") != true )
            {
                // if Exits size is more zero or not
                if(RB_Constant.exitsrestaurants_obj.response.size() > 0){
                    if(RB_Constant.exitsrestaurants_obj.response.size() > 2){
                        if(RB_Constant.exitsrestaurants_obj.response.get(0).listRestaurants.size() > 0){                                                                                                                                
                            System.out.println("Exit3 Restaurants");
                            // Create the views on the fly instead of using the ListView
                            rbupcadapter3 = new UpcomingResultsListViewAdapter3(this);

                            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 restaurants size:"+numItems3);
                            }

                            //linearLayout2

                            for(int position=0; position < numItems3; position++)
                            {
                                View convertview = null;
                                convertview = rbupcadapter1.getView(position, convertview, null);
                                listLayout3.addView(convertview);                                                           
                            }                                                               

                        } else {
                            System.out.println("No results in Exit3");
                            //toastMsg("No results in Exit3");
                        }   
                    }   
                }
            }
        }
    }
    catch(JsonParseException e)
    {
        System.out.println("Exception in check_exitrestrepWSResponse::"+e.toString());
    }       
}


By using a try-catch-block?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜