onResponse()被调用两次

时间:2019-07-25 17:04:34

标签: android caching android-volley

在logcat之后,我发现以下响应被传递了两次,一次是从缓存中传递的(我忽略了parseCacheHeaders并实现了自定义缓存项),然后又从服务器传递了一次。但这只会在第一个请求之后发生一次,之后的任何请求都会从缓存的响应中传递一次。

   private JsonArrayRequest getProjectDetails(final Integer page) {
        swipeContainer.setRefreshing(true);
        request = new JsonArrayRequest(PROJECT_DETAIL, new Response
                .Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                System.out.println("onResponseProjectFrag");
                parseResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                }
        }) {
            @Override
            protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {

                try {
                    final long cacheHitButRefreshed = 5 * 60 * 1000; // in 3 minutes cache will
                    // hit but also refresh from server.
                    final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
                    long now = System.currentTimeMillis();
                    final long softExpire = now + cacheHitButRefreshed;
                    final long ttl = now + cacheExpired;
                    cacheEntry.data = response.data;
                    cacheEntry.softTtl = softExpire;
                    cacheEntry.ttl = ttl;
                    String headerValue = response.headers.get("Date");
                    if (headerValue != null) {
                        cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    headerValue = response.headers.get("Last-Modified");
                    if (headerValue != null) {
                        cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
                    }
                    cacheEntry.responseHeaders = response.headers;
                    String jsonString = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers));
                    return Response.success(new JSONArray(jsonString), cacheEntry);
                } catch (UnsupportedEncodingException | JSONException e) {
                    return Response.error(new ParseError(e));
                }
            }
            @Override
            public Map<String, String> getHeaders() {
                Map<String, String> header = new HashMap<String, String>();
                header.put("accept-language", "en");
                header.put("Authorization", "Bearer " + LoginActivity.TOKEN);
                return header;
            }

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("page", page.toString());
                return params;
            }
        };

        request.setShouldCache(true);
        return request;

0 个答案:

没有答案