Android Volley 不为标准 POST 请求调用 getParams()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24284651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:58:54  来源:igfitidea点击:

Volley not calling getParams() for standard POST request

androidandroid-volley

提问by Varun Jain

I am trying to post some parameters to my rails API using Volley in Android. This is the code:

我正在尝试在 Android 中使用 Volley 将一些参数发布到我的 rails API。这是代码:

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()?

我尝试了两个日志语句,一个getParams()getHeaders(). 一个getHeaders()已登录,而另一个未登录。为什么凌空无视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);

}

回答by Ali Azhar

Using StringRequest in place of JsonObjectRequest

使用 StringRequest 代替 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);

回答by josh123a123

The third parameter should be a JSONObject you do not need the getParams() method just pass them into the request.

第三个参数应该是一个 JSONObject 你不需要 getParams() 方法只需将它们传递到请求中。

 JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            method,
            url,
            jsonObjParams,    // <<< HERE
            responseListener,
            errorListener);

回答by fansan

it happened because Volley params cache.

这是因为 Volley params 缓存。

clean it like this

像这样清洁

requestQueue.getCache().clear();

requestQueue.getCache().clear();

hope it's useful!

希望它有用!

回答by idsider

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.

对于 JSONobjectRequest,getParams() 不适用于 POST 请求,因此您必须在那里创建 customRequest 并覆盖 getParams() 方法。因为 JsonObjectRequest 是扩展 JsonRequest 直接覆盖 getBody() 方法,所以你的 getParam() 永远不会调用。要调用 getParams() 方法,您首先必须覆盖 getBody()。或者对于一个简单的解决方案,您可以使用 StringRequest。

回答by bigtree

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.

请覆盖 getBody() 方法,如果您的服务器无法处理 JSON 请求参数,则必须覆盖 getHeaders() 方法以更改您的 Content-Type。

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

问题可以在这里找到:https: //github.com/mcxiaoke/android-volley/issues/82

回答by Rakshita

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

我通过简单地从标题中删除 Content-Type 解决了我的问题:)

回答by Yasser

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

我遇到了同样的问题,我使用清除队列缓存解决了它

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.getCache().clear();
requestQueue.add(stringRequest);

回答by SergeA

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

shouldCache(false)在将请求对象添加到队列之前尝试使用它。

回答by Abu Yousuf

To provide POSTparameter build a JSONObjectwith your POST parameters and pass that JSONObjectas a 3rd parameter. JsonObjectRequestconstructor accepts a JSONObjectin constructor which is used in Request Body.

要提供POST参数JSONObject,请使用您的 POST参数构建一个并将其JSONObject作为第三个参数传递。JsonObjectRequest构造函数接受JSONObject在请求正文中使用的构造函数。

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);

回答by sigit dwi

if you use My Singleton try this:

如果你使用 My Singleton 试试这个:

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

Maybe you use cache on your code

也许您在代码上使用缓存