Http Status Code in Android Volley When Error.Networkresponse Is Null

Http Status Code in Android Volley when error.networkResponse is null

401 Not Supported by Volley

It turns out that it is impossible to guarantee that error.networkResponse is non-null without modifying Google Volley code because of a bug in Volley that throws the Exception NoConnectionError for Http Status Code 401 (HttpStatus.SC_UNAUTHORIZED) in BasicNetwork.java (134) prior to setting the value of networkResponse.

Work-Around

Instead of fixing the Volley code, our solution in this case was to modify the Web Service API to send Http Error Code 403 (HttpStatus.SC_FORBIDDEN) for the particular case in question.

For this Http Status Code, the value of error.networkResponse is non-null in the Volley error handler: public void onErrorResponse(VolleyError error). And, error.networkResponse.httpStatusCode correctly returns HttpStatus.SC_FORBIDDEN.

Other-Suggestions

Rperryng's suggestion of extending the Request<T> class may have provided a solution, and is a creative and excellent idea. Thank you very much for the detailed example. I found the optimal solution for our case is to use the work-around because we are fortunate enough to have control of the web services API.

I might opt for fixing the Volley code in one location within BasicNetwork.java if I did not have access to making a simple change at the server.

Getting response code from volley error response

Could you try:

int statusCode = error.networkResponse.statusCode;
Log.e(TAG, String.valueOf(statusCode);

how to handle statuscode in android studio?

new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {

Log.e("Volly Error", error.toString());
// Dismiss progress dialog here

NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.statusCode == 401) {
// Put your code here to handle this specific error code
}
}

}

Android. Volley. volleyError.networkResponse is null

So I've tested it on devices before 4.3 and it seems it doesn't work, on the ones after 4.3. it does.

I've asked the backend guy to change the error response from 401 to 403 and it seems it works now. I've read somewhere that the header for 401 might be wrong or might not contain the appropriate data and it makes the code co berzerk.

The exact reasonn for this is unknown to me. We've left it as a 403 error and everything's fine.

Volley Error Response Gives Null

This

if (error == null) {
if (error.getClass().equals(TimeoutError.class)){
Toast.makeText(getApplicationContext(),"Time Out Error",Toast.LENGTH_LONG).show();
}
}

Should be

if (error != null) {
if (error.getClass().equals(TimeoutError.class)) {
Toast.makeText(getApplicationContext(),"Time Out Error",Toast.LENGTH_LONG).show();
}
}

first fix this!

Checking status code in Volley error listener is not working in Android

I found the issue with that code. Status code is correctly returned if the server returned stats like 500,400 and so on except 401. But when server returns status code 401, network response is always null. So I just considered like, if the status returned 0, it is equal to 401. That is how I solved the problem with my app.

Volley Error Message null in logcat

Try this .

@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Failed with error msg:\t" + error.getMessage());
Log.d(TAG, "Error StackTrace: \t" + error.getStackTrace());
// edited here
try {
byte[] htmlBodyBytes = error.networkResponse.data;
Log.e(TAG, new String(htmlBodyBytes), error);
} catch (NullPointerException e) {
e.printStackTrace();
}
if (error.getMessage() == null){
createUser();
}
}


Related Topics



Leave a reply



Submit