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).
you have to override parseNetworkError and deliverError methods and you can get errormessage from them.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With