Reputation: 3284
I using API for login, if login success it return code 200 with respone. and if incorrect email or password will return code 401 with json object contain errors
the problem is that I want to get the network response even the request code not equal 200 like the image
but volley throw exception BasicNetwork.performRequest: Unexpected response code 401
here is my code
public interface OnResponse{
void onResponse(JSONObject response) throws Exception;
void onErrorResponse(VolleyError volleyError);
}//OnResponse
public static void newRequest(Context context, String url, final Map<String, String>params, final OnResponse onResponse){
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RequestQueue queue = Volley.newRequestQueue(context);
//
//onErrorResponse()
//onResponse()
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, response -> {
// - - - - - - - - - - - - - - - - - - - - -
try {
JSONObject object = new JSONObject(response);
onResponse.onResponse(object);
} catch (Exception e) {
Log.e(TAG, "FLAG-1");
e.printStackTrace();
}
// - - - - - - - - - - - - - - - - - - - - -
}, error -> {
Log.e(TAG, "FLAG-2");
onResponse.onErrorResponse(error);
}){
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
volleyError.printStackTrace();
Log.e("zxc", volleyError.getMessage()+"");
Log.e("zxc", volleyError.getLocalizedMessage()+"");
return super.parseNetworkError(volleyError);
}
};
//
stringRequest.setParams(params);
stringRequest.setShouldCache(false);
queue.add(stringRequest);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
}//newRequest()`
Upvotes: 3
Views: 1886
Reputation: 3708
If the network status code is other than 4xx or 5xx, Volley returns the response in the onErrorResponse
as VolleyError
. The response is received as a byte array so, you need to parse it to read the error and the message keys.
Below is a trimmed version of the required code.
final String failAuthError = new String(volleyError.networkResponse.data, Charset.forName("UTF-8"));
ErrorResponse errorResponse = new Gson().fromJson(failAuthError, ErrorResponse.class);
Log.e(TAG, "Error Status: " + errorResponse.error);
Log.e(TAG, "Error Message: " + errorResponse.message);
and ErrorResponse
is a custom class with error
and message
variables.
class ErrorResponse {
@Expose
@SerializedName("error")
String error;
@Expose
@SerializedName("message")
String message;
}
Upvotes: 0
Reputation: 7490
You can extract response data from VolleyError
.
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.data != null) {
String jsonError = new String(networkResponse.data);
// Print Error!
}
}
Upvotes: 0
Reputation: 379
VolleyError
has the networkResponse
attribute, it contains the response.
This is an example: String response = new String(error.networkResponse.data);
Upvotes: 2
Reputation: 6297
Simply override parseNetworkResponse
in newRequest() method like below
StringRequest request = new StringRequest(Method.GET,
<your url>,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//handle response
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//handle error
}
}) {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
int mStatusCode = response.statusCode;
return super.parseNetworkResponse(response);
}
};
Upvotes: 0