Reputation: 254
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
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
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
Reputation: 940
Response Code 400 means Bad Request.
Some reasons for that.
Upvotes: 3
Reputation: 3802
Some possible causes of this error:
Upvotes: 1
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
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
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
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