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
How to Create Topic in Fcm Notifications
Android Device Chooser -- Device Not Showing Up
How to Check If an App Is a Non-System App in Android
How to Read and Edit Android Calendar Events Using the New Android 4.0 Ice Cream Sandwich API
Android Failed to Install Helloworld.Apk on Device (Null) Error
Extended Surfaceview's Ondraw() Method Never Called
How to Populate Android Room Database Table on First Run
Android Studio Doesn't Recognize My Device
Can an Aar Include Transitive Dependencies
Android Finish() Method Doesn't Clear App from Memory
How to Declare Git Repository as Dependency in Android Gradle
Difference Between Extending Lifecycleactivity,Activity,Actionbaractivity & Appcompactactivity
Can You Launch the Native Camera App from an HTML 5 Web App
Difference Between a Clickable Imageview and Imagebutton
How to Implement Uncaughtexception Android
Android How to Create Triangle and Rectangle Shape Programmatically