Android Volley – BasicNetwork.performRequest: Unexpected response code 400


Problem statement:

I am trying to access an REST API that will return a JSON object for various HTTP status codes (400, 403, 200 etc) using Volley.

For any HTTP status other than 200, it seems the 'Unexpected response code 400' is a problem. Does anyone have a way to bypass this 'error'?


protected void getLogin() {   
    final String mURL = "";

    EditText username = (EditText) findViewById(;
    EditText password = (EditText) findViewById(;

    // Post params to be sent to the server
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("username", username.getText().toString());
    params.put("password", password.getText().toString());

    JsonObjectRequest req = new JsonObjectRequest(mURL, new JSONObject(
            params), new Response.Listener<JSONObject>() {
        public void onResponse(JSONObject response) {

            try {
                JSONObject obj = response

                        "status code..." + obj.getString("name"));

                // VolleyLog.v("Response:%n %s", response.toString(4));

            } catch (JSONException e) {
    }, new Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {
            Log.w("error in response", "Error: " + error.getMessage());

    // add the request object to the queue to be executed

Best Answer

One way of doing this without changing Volley's source code is to check for the response data in the VolleyError and parse it your self.

As of f605da3 commit, Volley throws a ServerError exception that contains the raw network response.

So you can do something similar to this in your error listener:

/* import; */
public void onErrorResponse(VolleyError error) {

    // As of f605da3 the following should work
    NetworkResponse response = error.networkResponse;
    if (error instanceof ServerError && response != null) {
        try {
            String res = new String(,
                       HttpHeaderParser.parseCharset(response.headers, "utf-8"));
            // Now you can use any deserializer to make sense of data
            JSONObject obj = new JSONObject(res);
        } catch (UnsupportedEncodingException e1) {
            // Couldn't properly decode data to string
        } catch (JSONException e2) {
            // returned data is not JSONObject?

For future, if Volley changes, one can follow the above approach where you need to check the VolleyError for raw data that has been sent by the server and parse it.

I hope that they implement that TODO mentioned in the source file.