Volley onErrorResponse Give NullPointerException

后端 未结 3 1531
抹茶落季
抹茶落季 2021-01-13 17:39

i try volley library in my android application

this is my log

    10-31 14:30:09.277: E/AndroidRuntime(22916): java.lang.NullPointerException
10-31 1         


        
相关标签:
3条回答
  • 2021-01-13 17:58

    Apply this code hope that this will help

    @Override public void onErrorResponse(VolleyError volleyError) {

      Log.v("VolleyError",volleyError.getMessage);
    
    });
    

    This will print error in your log

    0 讨论(0)
  • 2021-01-13 18:00

    Chances are that volleyError.networkResponse.data is empty. I am not sure what you are trying to get with this line of code, but working with Volley and wanting to see what is in volleyError. You could try this:

    String error =  volleyError.toString();
    

    You can then check this string for any specific errors [at least that's how I do it]. VolleyErrors could be one of the few defined by the API such as timeout error, connection error, server error, and so forth. Of-course, you might have to parse the string further if you want to fire other actions based on a specific error.

    0 讨论(0)
  • 2021-01-13 18:03

    It seems like onErrorResponse responds differently on few devices

    onErrorResponse returned null on few devices (that was the reason for the crash)

      new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                        if(error.getMessage==NULL){
      Toast.makeText(cardview.this, "Failed to retrieve data", Toast.LENGTH_LONG).show();
                        }
                    else{
                            Toast.makeText(cardview.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
                        }
    
                    });
    

    I also had the same error..it varies from device to device ...you may find that it won't give nullpointer exception on some devices.

    0 讨论(0)
提交回复
热议问题