【问题标题】:Volley Post JsonObjectRequest ignoring parameters while using getHeader and getParamsVolley Post JsonObjectRequest 在使用 getHeader 和 getParams 时忽略参数
【发布时间】:2014-08-25 14:14:04
【问题描述】:

我正在尝试连接 API url="api adress",它接受两种标头类型 application/json 以响应 json 和 application/xml 以响应 xml。我需要用 json 参数打 JSON,响应也将是 json 格式。使用带有 JsonObjectRequest 设置标头的 android volley Post 请求使用 getHeaders 连接到服务器,但 getParams 设置参数不起作用。

RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Constants.BASE_URL + Constants.LOGIN, null, response,
            response_error) {
        /**
         * Passing some request headers
         * */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            return headers;
        }
         @Override
         protected Map<String, String> getPostParams()
         throws AuthFailureError {
         // TODO Auto-generated method stub
            Map<String, String> params = new HashMap<String, String>();
            params.put("key", "value");
            return params;
         }
    };
    // implementation of listeners
    Response.Listener<JSONObject> response = new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, response.toString());
            Log.e("responce", response.toString());

            // msgResponse.setText(response.toString());
            hideProgressDialog();
        }
    };
    Response.ErrorListener response_error = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("error responce", error.getMessage());
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hideProgressDialog();
        }
    };
//get params never get called
//i also tried alternative to send params but does not works.
        Map<String, String> params = new HashMap<String, String>();
            params.put("key", "value");
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
            Constants.BASE_URL + Constants.LOGIN, new JSONObject(params), response,
            response_error) {
        /**
         * Passing some request headers
         * */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            return headers;
        }
};

    any type of help will be appretiated, Thanks in advance.

【问题讨论】:

    标签: android json post content-type android-volley


    【解决方案1】:

    Volley 会忽略你的Content-Type 设置,如果你想修改内容类型,你可以覆盖getBodyContentType 方法:

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, url,
            new JSONObject(params), response, response_error) {
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }
    }
    

    为什么凌空忽略你的参数?看看我的另一个answer

    【讨论】:

      【解决方案2】:

      Volley JsonObjectRequest Post request not working

      从该帖子中获取另一个答案,开头是:您可以创建自定义 JSONObjectReuqest

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-14
        • 2016-01-23
        • 1970-01-01
        • 2015-06-09
        • 1970-01-01
        • 2019-01-26
        • 2015-08-10
        • 1970-01-01
        相关资源
        最近更新 更多