Asif Ali Asif Ali - 3 months ago 18
Android Question

Code response code 400 when sending POST params and headers using Volley

I have tried every single method available on stackoverflow but result remains the same.


I want to send params and also api key in the Authorization field of header using volley.


Here is the code snippet.

holder.comment_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String POSTCOMMENT_URL = Utility.getIPADDRESS()+"comments";
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, POSTCOMMENT_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("Response: " , response.toString());
Toast.makeText(myApplication.getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(myApplication.getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
}
}){
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Authorization", getApiKey() );
return params;
}

@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("pid", "39");
params.put("comment", "mobile testing");
return params;
}

};
requestQueue.add(jsonObjectRequest);
}
});


I'm getting this error:


[5333] BasicNetwork.performRequest: Unexpected response code 400 for http://www.XXXXXXXXXXX.com/comments


I have already tested the endpoint using Advanced Rest client, it works and returns the JsonObject with previously added comment.

Here are the results:

{
"error": false
"message": "comment added successfully added!"
"comment_id": "12"
"user_id": "12"
"pid": "39"
"comment": "testing for stackoverflow"
"date of comment": "2016-07-30 13:49:08"
}

Answer

Ok, after a long research and stupid rather valuable changes, it finally worked for me.

Here is the working code:

holder.comment_button.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(final View view) {

           final String api_key = getApiKey();
           String POSTCOMMENT_URL = Utility.getIPADDRESS()+"comments";
           volleySingleton = VolleySingleton.getInstance();
           requestQueue = volleySingleton.getRequestQueue();
           StringRequest stringRequest = new StringRequest(Request.Method.POST, POSTCOMMENT_URL, new Response.Listener<String>() {
               @Override
               public void onResponse(String response) {
                    Toast.makeText(myApplication.getApplicationContext(),response,Toast.LENGTH_LONG).show();
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   Toast.makeText(myApplication.getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show();
               }
           }) {

               @Override
               public Map<String, String> getHeaders() throws AuthFailureError {
                   Map<String, String>  params = new HashMap<>();
                   params.put("Authorization", api_key);
                   return params;
               }

              @Override
               protected Map<String, String> getParams() {
                   Map<String, String>  params = new HashMap<>();
                   params.put("pid", "40");
                   params.put("comment", "testing");
                   return params;
               }

           };
           requestQueue.add(stringRequest);
       }
   });

Note: It worked for me, I'm not sure about others.

Changes are: Change JsonObjectRequest to StringRequest and removing any Content-Type doesn't make any difference, so I removed it.

Also, make sure all the params have some values i.e test it by logging.