Volley Post JsonObjectRequest 在使用 getHeader 和 getParams 时忽略参数 [英] Volley Post JsonObjectRequest ignoring parameters while using getHeader and getParams

查看:19
本文介绍了Volley Post JsonObjectRequest 在使用 getHeader 和 getParams 时忽略参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to connect API url="api adress" which accepts two header types application/json to reponce in json and application/xml to reponce in xml. I need to hit JSON with json parameters and responce will be in json format too. Using android volley Post request with JsonObjectRequest setting headers using getHeaders it connects to server but getParams to set parameters does not work.

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.

推荐答案

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

Volley will ignore your Content-Type setting, if you want to modify the content-type, you can override the getBodyContentType method :

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

为什么 volley 忽略你的参数?看看我的另一个答案.

for why volley ignore your parameters? take a look at my another answer.

这篇关于Volley Post JsonObjectRequest 在使用 getHeader 和 getParams 时忽略参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆