而使用getHeader和getParams能够截击后JsonObjectRequest忽略参数参数、getHeader、getParams、JsonObjectRequest

2023-09-06 01:16:58 作者:折月煮酒゜

我试图连接API URL =API住址」接受两个头类型的应用程序/ JSON的JSON和应用程序/ XML来reponce在XML中reponce。我需要打JSON使用JSON的参数和性反应将是JSON格式了。使用使用getHeaders它连接到服务器,但getParams能够JsonObjectRequest设置页眉设置参数不起作用的Andr​​oid凌空发布采购信息。

 请求队列排队= Volley.newRequestQueue(本);
JsonObjectRequest jsonObjReq =新JsonObjectRequest(Method.POST,
            Constants.BASE_URL + Constants.LOGIN,空,反应,
            response_error){
        / **
         *传递一些请求头
         * * /
        @覆盖
        公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
            HashMap的<字符串,字符串>标题=新的HashMap<字符串,字符串>();
            headers.put(内容类型,应用/ JSON);
            返回头;
        }
         @覆盖
         保护地图<字符串,字符串> getPostParams()
         抛出AuthFailureError {
         // TODO自动生成方法存根
            地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
            params.put(钥匙,价值);
            返回PARAMS;
         }
    };
    //执行听众
    Response.Listener<的JSONObject>响应=新Response.Listener<的JSONObject>(){

        @覆盖
        公共无效onResponse(JSONObject的响应){
            Log.d(TAG,response.toString());
            Log.e(的反响,response.toString());

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

        @覆盖
        公共无效onErrorResponse(VolleyError错误){
            Log.e(错误的反响,error.getMessage());
            VolleyLog.d(TAG,错误:+ error.getMessage());
            hideProgressDialog();
        }
    };
// GET参数永远不会叫
//我还试图替代发送PARAMS,但不工作。
        地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
            params.put(钥匙,价值);
        JsonObjectRequest jsonObjReq =新JsonObjectRequest(Method.POST,
            Constants.BASE_URL + Constants.LOGIN,新的JSONObject(PARAMS),对此,
            response_error){
        / **
         *传递一些请求头
         * * /
        @覆盖
        公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
            HashMap的<字符串,字符串>标题=新的HashMap<字符串,字符串>();
            headers.put(内容类型,应用/ JSON);
            返回头;
        }
};

    任何形式的帮助将是ptiated AP $ P $,在此先感谢。
 

解决方案

排球会忽略你的内容类型设置,如果要修改的内容类型,你可以覆盖 getBodyContentType 方法:

  JsonObjectRequest jsonObjReq =新JsonObjectRequest(Method.POST,网址,
        新的JSONObject(PARAMS),响应,response_error){
    @覆盖
    公共字符串getBodyContentType(){
        返回应用程序/ JSON的;字符集= UTF-8;
    }
}
 

为什么凌空忽略你的参数?看看我的另一回答。

javascript 的 .getJSON问题

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 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";
    }
}

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

 
精彩推荐
图片推荐