'Getting com.android.volley.ServerError using the Volley library. What are some possible causes?

When I make a request in Volley, I'm receiving com.android.volley.ServerError and response code 400.

I'm doing something like this (generic example code):

final String param1 = "...";
final String param2 = "...";
// etc. 

StringRequest stringRequest = new StringRequest(Request.Method.POST, API_URL,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this,error.toString(),Toast.LENGTH_LONG).show();
            }
        }){
    @Override
    protected Map<String,String> getParams(){
        Map<String,String> params = new HashMap<String, String>();
        params.put("PARAM_1", param1);
        params.put("PARAM_2",password);
        return params;
    }

};

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);

What are possible causes for this?



Solution 1:[1]

I had the same issue. It drained my entire day. Finally, I Found the tiny error, which was that I was passing a null value in parameters.

Check if you are passing a null value to the server in getParams() or your JSON Object. If yes, that may be the problem.

Solution 2:[2]

In below method

public void onErrorResponse(VolleyError error)

add this line:-

error.printStackTrace()

You will get all stack trace which gives you the actual error. I have also get this error but in my case it is actually TimeoutException.

Solution 3:[3]

Response Code 400 means Bad Request.

Some reasons for that.

  1. There might be issue in Request method. Is it GET or POST?
  2. API might be expecting Authorization Header.

Solution 4:[4]

You may also need to tell your server that send the response with the code 200, volley checks for this codes.

Solution 5:[5]

Some possible causes of this error:

  1. Wrong URL. Check your URL for any errors.
  2. Missing parameters, any parameters having null value or you are missing any parameter.
  3. Watch for the Keys defined in the params, it should match with those of APIs.

Solution 6:[6]

This error can mean BAD REQUEST, check the headers you are sending. Overring the getHeaders() method you can send the right headers.

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    return (headers != null || headers.isEmpty()) ? headers : super.getHeaders();
}

Also, usually the WebServices need application/json headers, as follow:

headers.put("Accept","application/json");
headers.put("Content-Type","application/json");

Solution 7:[7]

Double check your parameters (there should be no null parameter). If it's ok then If you are using JSONObjectRequest then remove Request.Method.GET as per new google policy.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Robert Columbia
Solution 2 halfer
Solution 3 Ryan M
Solution 4 Pianisimo
Solution 5 Ryan M
Solution 6 Ryan M
Solution 7 Ryan M