使用getHeader和getParams时,Volley Post JsonObjectRequest忽略参数

时间:2014-07-04 09:22:25

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

我正在尝试连接API url =“api adress”,它接受两个头类型application / json来响应json和application / xml以在xml中重新定义。我需要使用json参数命中JSON,并且响应也将采用json格式。使用android volley Post请求和JsonObjectRequest设置头使用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.

2 个答案:

答案 0 :(得分:13)

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

答案 1 :(得分:0)

Volley JsonObjectRequest Post request not working

从帖子的另一个答案开始:您可以创建自定义JSONObjectRequest