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

ars*_*tic 10 post android json 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.
Run Code Online (Sandbox Code Playgroud)

Vin*_*ing 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";
    }
}
Run Code Online (Sandbox Code Playgroud)

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


gre*_*pps 0

Volley JsonObjectRequest Post 请求不起作用

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