Volley库android - getHeaders()从未使用过&从不使用getParams()

时间:2016-08-31 10:38:36

标签: java android api httprequest android-volley

在这里,我想了解为什么getHeaders()getParams()函数未被使用和工作。

            private String tag_json_obj = "jobj_req";

            ProgressDialog pDialog = new ProgressDialog(this);

            String tag_json_obj = "json_obj_req";

            String url = "https://api.abc.xyz.com:3021/login";


            pDialog.setMessage("Loading...");
            pDialog.show();



            JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
                    url, obj,
                    new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d(AbcLogin.class.getSimpleName(), response.toString());
                            pDialog.hide();
                        }
                    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(AbcLogin.class.getSimpleName(), "Error: " + error.getMessage());
                    // hide the progress dialog
                    pDialog.hide();
                }
            });

            /**
             * 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> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("user", "abc12");
                params.put("password", "xyzan");

                return params;
            }

            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);

我必须在代码中传递标题和参数。

2 个答案:

答案 0 :(得分:2)

StringRequest stringObjectRequest = new StringRequest(httpMethod, url, new Listener<String>() {
           @Override
           public void onResponse(String response) {

           }
       }, new ErrorListener() {

           @Override
           public void onErrorResponse(VolleyError error) {
               if(error != null && error.getMessage() != null)
               {
                   Log.e("Response error", error.getMessage());
               }
           }
       }
       )
       {
           @Override
           protected Map<String, String> getParams() throws AuthFailureError {
               return params;
           }
           @Override
           public Map<String, String> getHeaders() throws AuthFailureError {
               return super.getHeaders();
           }
       };

答案 1 :(得分:0)

请检查您的代码是否使用缓存。

如果使用了缓存,它将仅在缓存过期时才允许执行getHeaders()方法,否则将不执行getHeaders()

请使用以下代码在凌空中进行缓存。

 public static Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) {


        long now = System.currentTimeMillis();
        Map<String, String> headers = response.headers;
        long serverDate = 0;
        String serverEtag = null;
        String headerValue;
        headerValue = headers.get("Date");
        if (headerValue != null) {
            serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
        }
        serverEtag = headers.get("ETag");

        final long cacheHitButRefreshed = 0; // in 0 millisecond cache will be hit, but also refreshed on background

        final long cacheExpired = 0; // in 0 millisecond this cache entry expires completely

        final long softExpire = now + cacheHitButRefreshed;
        final long ttl = now + cacheExpired;
        Cache.Entry entry = new Cache.Entry();
        entry.data = response.data;
        entry.etag = serverEtag;
        entry.softTtl = softExpire;
        entry.ttl = ttl;
        entry.serverDate = serverDate;
        entry.responseHeaders = headers;
        return entry;
    }