401 status code might come with a blank response data and if you check this volley code line 63-75
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
if jsonString is blank it still tries to create a JSONObject from it new JSONObject(jsonString)
which throws a JSONException
You may download volley source code, import it as module in your android studio project, add it dependency and make this correction:
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
// Fix for an error on blank success response
// return Response.success(new JSONObject(jsonString),
// HttpHeaderParser.parseCacheHeaders(response));
if (!jsonString.trim().contentEquals("")) {
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
}
else {
return Response.success(new JSONObject(),
HttpHeaderParser.parseCacheHeaders(response));
}
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
To add Volley to your project - clone the Volley repository and set it as a library project
Git clone the repository by typing the following at the command line:
git clone https://android.googlesource.com/platform/frameworks/volley
Import the downloaded source into your app project as an Android library module as described in Create an Android Library.
You may also try Prase 401 volley error message if you don't want a fix inside the imported library: