HttpResponseCache not working in Android Lollipop

前端 未结 2 1457
天命终不由人
天命终不由人 2021-01-03 06:46

I\'ve been using in my app HttpResponseCache successfully, but when my phone updated to Lollipop I realized that HttpResponseCache now never get \"hit\", always do the netwo

相关标签:
2条回答
  • 2021-01-03 07:04

    After having this problem for days I ran into this issue. It is fixed again in Marshmallow.

    It is a bug in lollipop where the Vary - > Accept-Encoding header breaks the cache because Accept-Encoding gets filled in by default but not written away.

    Here is a link to the issue:

    https://code.google.com/p/android/issues/detail?id=162475

    The fix is to set the Accept-Encoding explicitly:

    Accept-Encoding -> gzip

    or

    Accept-Encoding -> identity

    On the reading side you have to add this to the reading of the input stream:

    String encoding = urlConnection.getHeaderField("Content-Encoding");
    boolean gzipped = encoding!=null && encoding.toLowerCase().contains("gzip");
    Inputstream inputStream;
    if(gzipped)
        inputStream = new GZIPInputStream(urlConnection.getInputStream());
    else
        inputstream = urlConnection.getInputStream();
    
    0 讨论(0)
  • 2021-01-03 07:13

    I've had similar problem. I was expecting images to be cached but they weren't.

    What turned out to be the problem was that I was not closing InputStream after it being read into a Bitmap.

    Your fetchInputStream returns an InputStream that it got from http connection, make sure you close it properly.

    The android http cache will not save a resource until you close the connection's InputStream.

    0 讨论(0)
提交回复
热议问题