Mina Wissa
Mina Wissa

Reputation: 10971

Android: HttpResponseCache: retrieve cached content when there is no internet connection?

I want to use the HttpResponseCache

My scenario is as follows:

I tested it on the following GitHub API URL: https://api.github.com/users/blackberry/repos

my code is as follows

String loadData() {
            HttpURLConnection urlConnection = null;
            String response = null;
            try {
                URL url = new URL(
                        "https://api.github.com/users/blackberry/repos");
                urlConnection = (HttpURLConnection) url.openConnection();



                InputStream in = new BufferedInputStream(
                        urlConnection.getInputStream());
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(in));
                StringBuffer buffer = new StringBuffer();
                String s = null;
                while ((s = reader.readLine()) != null) {
                    buffer.append(s);
                }
                response = buffer.toString();
            } catch (MalformedURLException e) {
                Log.e("MalformedURLException", e.toString());
            } catch (IOException e) {
                Log.e("IOException", e.toString());
            } finally {
                urlConnection.disconnect();
                HttpResponseCache cache = HttpResponseCache.getInstalled();
                if (cache != null) {
                    cache.flush();
                }
            }
            return response;
        }

when I tested the offline mode, an IO exception was thrown at:

InputStream in = new BufferedInputStream(
                        urlConnection.getInputStream());

what can be wrong here ?

Upvotes: 2

Views: 1285

Answers (2)

Umesh
Umesh

Reputation: 1609

This is too late but may help someone. I was facing same problem because when network is available it is not forcefully getting data from server and getting cached data.

  if (IsNetAvailable(context)) {
                urlConnection.addRequestProperty("Cache-Control", "max-age=0");
                //   urlConnection.setUseCaches(true);
            } else {
               int maxStale = 60 * 10; // tolerate 10 mins
                urlConnection.addRequestProperty("Cache-Control", "max-stale=" + maxStale);
                urlConnection.setUseCaches(true);
            }

I have used same code and working fine for following requirement.

1. Get data from server forcefully when network available.

2. Get data from cache when offline.

Upvotes: 1

kAnNaN
kAnNaN

Reputation: 3679

So what you're doing is trying to get the data from the http connection object even if there is no internet connection.

Rather before making a request. You should check for internet connection and the make the call from cache if there is no connection else make an http request.

Example :

URL url = new URL(
                        "https://api.github.com/users/blackberry/repos");
                urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = null;
if(//Check for internet connection here){

                in = new BufferedInputStream(
                            urlConnection.getInputStream());
    }else{
//load the data from cache
     urlConnection.addRequestProperty("Cache-Control", "only-if-cached");
             in = urlConnection.getInputStream();
    }
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(in));
                    StringBuffer buffer = new StringBuffer();
                    String s = null;
                    while ((s = reader.readLine()) != null) {
                        buffer.append(s);

Upvotes: 0

Related Questions