Volley not calling getParams() for standard POST request

后端 未结 11 925
执念已碎
执念已碎 2020-11-30 07:56

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

相关标签:
11条回答
  • 2020-11-30 08:34

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

    0 讨论(0)
  • 2020-11-30 08:37

    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);
    
    0 讨论(0)
  • 2020-11-30 08:38

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

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.getCache().clear();
    requestQueue.add(stringRequest);
    
    0 讨论(0)
  • 2020-11-30 08:39

    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.

    0 讨论(0)
  • 2020-11-30 08:43

    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);
    
    0 讨论(0)
提交回复
热议问题