18

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 another in getHeaders(). The one in getHeaders() is logged while the other one is not. Why is volley ignoring getParams()?

{
//full_name,email,password are private variables defined for this class

String url =  "http://10.0.2.2:3000/users/sign_up.json" ; 

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                 url, null,
                 new Response.Listener<JSONObject>() {

                     @Override
                     public void onResponse(JSONObject response) {
                         Log.d(TAG, response.toString());
                         pDialog.hide();
                     }
                 }, new Response.ErrorListener() {

                     @Override
                     public void onErrorResponse(VolleyError error) {
                         VolleyLog.d(TAG, "Error: " + error.getMessage());
                         pDialog.hide();
                     }
                 }) {

             @Override
             public Map<String, String> getParams() {
                 Map<String, String> params = new HashMap<String, String>();

                 //This does not appear in the log
                 Log.d(TAG,"Does it assign params?") ;


                 params.put("name", full_name.getText().toString());
                 params.put("email",email.getText().toString());
                 params.put("password", password.getText().toString());

                 return params;
             }
             @Override
             public Map<String, String> getHeaders() throws AuthFailureError {

                 //This appears in the log  
                 Log.d(TAG,"Does it assign headers?") ;

                 HashMap<String, String> headers = new HashMap<String, String>();
                 headers.put("Content-Type", "application/json; charset=utf-8");

                 return headers;
             }

         };

        // Adding request to request queue
        VHelper.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);

}
Varun Jain
  • 1,901
  • 7
  • 33
  • 66

12 Answers12

31

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);
Ali Azhar
  • 1,703
  • 19
  • 14
  • 4
    I can't believe this was it. I was using a JSONObjectRequest and the debugger never hit the getParams() method, so no POST params. The moment i changed to StringRequest the debugger hit the getParams(). ¬_¬ – Unknownweirdo Feb 19 '15 at 10:23
  • 3
    Thanks, you saved my day. Is there a reason why for JsonObjectRequest the overloaded getParams method is not getting invoked ? – Ajay Jul 15 '15 at 18:10
  • 3
    The JsonObjectRequest is extended JsonRequest which override getBody() method directly, so your getParam() would never invoke, – Mehroz Munir Dec 21 '15 at 09:26
  • Same here... Thank you! – Silas Jun 07 '16 at 14:01
  • thumbs up if you too got the problem – Rishabh Agrawal Jun 22 '16 at 06:52
  • Can someone post the reason why JSONObjectRequest is not hitting getParams() but StringRequest is? – aB9 Apr 19 '17 at 07:35
3

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);
Tony
  • 2,242
  • 22
  • 33
josh123a123
  • 2,070
  • 1
  • 13
  • 21
3

it happened because Volley params cache.

clean it like this

requestQueue.getCache().clear();

hope it's useful!

fansan
  • 73
  • 7
  • I tried this but didn't work to solve the issue of getParams() not being called. \n\n RequestQueue queue = Volley.newRequestQueue(this); queue.getCache().clear(); – Gene Mar 18 '22 at 15:21
2

I solved my issue by simply removing Content-Type from header :)

Rakshita
  • 61
  • 3
1

Please override the getBody() method, and if your server can not handle JSON request parameter, you have to override the getHeaders() method to change your Content-Type.

Issue can found here: https://github.com/mcxiaoke/android-volley/issues/82

Samad
  • 1,776
  • 2
  • 20
  • 35
bigtree
  • 43
  • 5
1

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.

idsider
  • 11
  • 1
0

Try using shouldCache(false) for your request object before you add it into queue.

Jason Roman
  • 8,146
  • 10
  • 35
  • 40
SergeA
  • 1
  • 2
0

To provide POST parameter build a JSONObject with your POST parameters and pass that JSONObject as a 3rd parameter. JsonObjectRequest constructor accepts a JSONObject in constructor which is used in Request Body.

JSONObject paramJson = new JSONObject();

paramJson.put("key1", "value1");
paramJson.put("key2", "value2");


JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url,paramJson,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
requestQueue.add(jsonObjectRequest);
Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
0

I had the same problem I solved it using clear queue Cache

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.getCache().clear();
requestQueue.add(stringRequest);
Yasser
  • 1,159
  • 1
  • 14
  • 19
0

just use requestQue.getCache().clear();

0

If you do a Post using Android Volley, it should call the methods getHeaders(), getBody(), getParams(), and getBodyContentType() in that order.

If you do a GET request using Android Volley, it should call the methods getHeaders() only.

Gene
  • 10,819
  • 1
  • 66
  • 58
-1

if you use My Singleton try this:

MyVolley.getInstance(this).getRequestQueue().getCache().clear();

Maybe you use cache on your code

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
sigit dwi
  • 31
  • 1