ResponseCache从未调用过

时间:2013-06-13 15:25:53

标签: java android caching https bitmap

我正在尝试使用此解决方案Android image caching在Android应用中缓存图片。我已经实现了一个特定的ResponseCache并覆盖了get和put方法。

尽管如此,图像没有正确缓存。当我调试时,我可以看到我的ResponseCache实现的put方法从未被调用过。每次发出请求时都会正确调用我的get方法但从不是put方法。什么都不会被缓存,所以它无法检索任何文件......

我的请求使用HTTPS,所以我想知道是否允许缓存响应,或者每次我想显示图像时是否必须处理请求服务器。

以下是代码:

public class ImageResponseCache extends ResponseCache {

    File cacheDir;

    public ImageResponseCache(File cacheDir) {
        super();
        this.cacheDir = cacheDir;
    }

    @Override
    public CacheResponse get(URI uri, String s,
    Map<String, List<String>> headers) throws IOException {
        final File file = new File(cacheDir, escape(uri.getPath()));
        if (file.exists()) {
            return new CacheResponse() {
                @Override
                public Map<String, List<String>> getHeaders()
                throws IOException {
                    return null;
                }

                @Override
                public InputStream getBody() throws IOException {
                    return new FileInputStream(file);
                }
            };
            } else {
            return null;
        }
    }

    @Override
    public CacheRequest put(URI uri, URLConnection urlConnection)
    throws IOException {
        Log.i("Image Response", "PUT");
        final File file = new File(cacheDir, escape(urlConnection.getURL()
        .getPath()));
        return new CacheRequest() {
            @Override
            public OutputStream getBody() throws IOException {
                return new FileOutputStream(file);
            }

            @Override
            public void abort() {
                file.delete();
            }
        };
    }

    private String escape(String url) {
        return url.replace("/", "-").replace(".", "-");
    }
}

以下是在适配器中请求我的图像的功能:

private Bitmap requestImage(String file) {
    Bitmap bm = null;

    Log.d(TAG, "path: " + file);
    URL url = null;
    HttpURLConnection http = null;

    try {

        url = new URL(file);

        if (url.getProtocol().toLowerCase().equals("https")) {
            NetworkUtils.trustAllHosts();
            HttpsURLConnection https = (HttpsURLConnection) url
                    .openConnection();
            https.setHostnameVerifier(NetworkUtils.DO_NOT_VERIFY);
            http = https;
        } else {
            http = (HttpURLConnection) url.openConnection();
        }
        http.setUseCaches(true);

        ResponseCache.setDefault(new ImageResponseCache(
                ImageAdapter.this.mContext.getCacheDir()));

        http.setRequestProperty(
                "Authorization",
                "Basic "
                        + Base64.encodeToString(
                                (Constants.USER + ":" + Constants.PASSWORD)
                                        .getBytes(), Base64.NO_WRAP));

        http.setConnectTimeout(Constants.TIME_OUT);

        bm = BitmapFactory.decodeStream((InputStream) http.getContent());

    } catch (Exception e) {
        Log.e(TAG, e.getMessage(), e);
    }

    return bm;
}

有谁知道可能出现什么问题?

0 个答案:

没有答案