I am trying to post some parameters to my rails API using Volley in Android. This is the code:
I tried with two log statements, one in getParams()
and
Try using shouldCache(false)
for your request object before you add it into queue.
Using StringRequest in place of JsonObjectRequest
StringRequest sr = new StringRequest(Request.Method.POST, url , new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, ""+error.getMessage()+","+error.toString());
}
}){
@Override
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("id", "28");
params.put("value", "1");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/x-www-form-urlencoded");
headers.put("abc", "value");
return headers;
}
};
AppController.getInstance().addToRequestQueue(sr);
I had the same problem I solved it using clear queue Cache
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.getCache().clear();
requestQueue.add(stringRequest);
For JSONobjectRequest the getParams() doesn’t work for POST requests so you have to make a customRequest and override getParams() method over there. Its because JsonObjectRequest is extended JsonRequest which overrides getBody() method directly, so your getParam() would never invoke. To invoke your getParams() method you first have to override getBody(). Or for a simple solution you can use StringRequest.
The third parameter should be a JSONObject you do not need the getParams() method just pass them into the request.
JsonObjectRequest jsonObjReq = new JsonObjectRequest(
method,
url,
jsonObjParams, // <<< HERE
responseListener,
errorListener);