Vikas Gaurav
Vikas Gaurav

Reputation: 254

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?

Upvotes: 8

Views: 46050

Answers (8)

Muhammad Afzal
Muhammad Afzal

Reputation: 158

I encountered a similar issue, but then I realized that my URL wasn't using HTTPS; it was HTTP. Once I switched to HTTPS, it worked perfectly. Please double-check to ensure you don't have the same oversight

Upvotes: 0

Alejandro Traver
Alejandro Traver

Reputation: 79

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");

Upvotes: 1

Code-Warrior
Code-Warrior

Reputation: 940

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.

Upvotes: 3

Sachin Soma
Sachin Soma

Reputation: 3802

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.

Upvotes: 1

Shahzad Ahmed
Shahzad Ahmed

Reputation: 1

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.

Upvotes: 0

Pianisimo
Pianisimo

Reputation: 109

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

Upvotes: 1

Anil Ravsaheb Ghodake
Anil Ravsaheb Ghodake

Reputation: 1596

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.

Upvotes: 7

Umasankar
Umasankar

Reputation: 505

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.

Upvotes: 8

Related Questions