A
A
afna2016-01-04 16:18:56
Java
afna, 2016-01-04 16:18:56

How to get cache using HttpUrlConnection?

Happy New Year to all!!!
In the Application class I do the following:

try {
            File httpCacheDir = new File(getCacheDir(), "http");
            long httpCacheSize = 30 * 1024 * 1024; // 30 MiB
            HttpResponseCache.install(httpCacheDir, httpCacheSize);
        } catch (IOException e) {
            if(BuildConfig.DEBUG)
                Log.v("my app", "HTTP response cache installation failed:" + e);
        }

I use HttpUrlConnection to get data from the server.
URL url = new URL("some url addess");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setUseCaches(true);
        urlConnection.setDoInput(true);
        urlConnection.addRequestProperty("Cache-Control", "max-state=2419200");
        InputStream inputStream = urlConnection.getInputStream();

I get an error:
libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable) Can
you please tell me how to use the cache in the absence of the Internet?
Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question