Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the response code from a VolleyError?

I'm looking for a way to get the response code of a thrown VolleyError. My ErrorListener looks like this:

Response.ErrorListener errorListener = new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
        //Get response code here
        VolleyLog.e("Error: ", error.toString());
        VolleyLog.e("Error: ", error.getLocalizedMessage());
    }
};

The 2 lines I send to my VolleyLog print the following:

03-12 10:57:56.932: E/Volley(7147): [1] 1.onErrorResponse: Error: 
03-12 10:57:56.932: E/Volley(7147): [1] 1.onErrorResponse: Error: 

Volley does know what comes back because I can also see the following in my VolleyLog:

03-12 10:57:56.692: E/Volley(7147): [41854] BasicNetwork.performRequest: Unexpected response code 403 for https://*******/Employee/authenticate
03-12 10:57:56.897: E/Volley(7147): [41854] BasicNetwork.performRequest: Unexpected response code 403 for https://*******/Employee/authenticate
03-12 10:57:56.902: W/System.err(7147): com.android.volley.AuthFailureError
03-12 10:57:56.902: W/System.err(7147):     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:143)
03-12 10:57:56.902: W/System.err(7147):     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)

So basically, what I want is to get that response code "403" (Forbidden).

like image 878
Bart Burg Avatar asked Mar 12 '14 10:03

Bart Burg


People also ask

How do I get volley error code?

you have to override parseNetworkError and deliverError methods and you can get errormessage from them.

How does volley handle 401 error in Android?

check first if the error is an instance of NoConnectionError or any other error type which does not contain a networkResponse. In the other case, networkResponse should be set and also should contain the expected data.


1 Answers

through the VolleyError:

error.networkResponse.statusCode

looking at the source code, I saw that VolleyError has a public final member NetworkResponse called networkResponse, that holds that statusCode of the Http call. You should check for NPE.

Here you can find the source code for the NetworkResponse. Here you can find the source code for the VolleyError

like image 66
Blackbelt Avatar answered Sep 28 '22 06:09

Blackbelt