I am trying to cache HTTP responses from my company’s API, but it looks like the application cannot access the cache directory:
W / System.err: could not be deleted: ENOENT (There is no such file or directory): / data / user / 0 / com.appname / cache / cache_file / journal.tmp
Tue / System.err: java.net.UnknownHostException: cannot resolve host "www.domain.com": no address associated with host name
I followed this tutorial . This is how I configure Retrofit (2.1.0):
import lu.CompanyName.R; import lu.CompanyName.interfaces.CompanyNameAPI; import okhttp3.Cache; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; import static okhttp3.logging.HttpLoggingInterceptor.Level.HEADERS; public class Injector { private static final String CACHE_CONTROL = "Cache-Control"; private static Retrofit provideRetrofit (String baseUrl) { return new Retrofit.Builder() .baseUrl(baseUrl) .client(provideOkHttpClient()) .addConverterFactory(GsonConverterFactory.create()) .build(); } private static OkHttpClient provideOkHttpClient () { return new OkHttpClient.Builder() .addInterceptor(provideHttpLoggingInterceptor()) .addInterceptor(provideOfflineCacheInterceptor()) .addNetworkInterceptor(provideCacheInterceptor()) .cache(provideCache()) .build(); } private static Cache provideCache () { return new Cache(new File(CompanyName.getInstance().getCacheDir(), "cache_file"), 20 * 1024 * 1024); } private static HttpLoggingInterceptor provideHttpLoggingInterceptor () { HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HEADERS); return httpLoggingInterceptor; } private static Interceptor provideCacheInterceptor () { return new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request originalRequest = chain.request(); String cacheHeaderValue = CompanyName.getInstance().checkIfHasNetwork() ? "public, max-age=2419200" : "public, only-if-cached, max-stale=2419200" ; Request request = originalRequest.newBuilder().build(); Response response = chain.proceed(request); return response.newBuilder() .removeHeader("Pragma") .removeHeader("Cache-Control") .header("Cache-Control", cacheHeaderValue) .build(); } }; } private static Interceptor provideOfflineCacheInterceptor () { return new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request originalRequest = chain.request(); String cacheHeaderValue = CompanyName.getInstance().checkIfHasNetwork() ? "public, max-age=2419200" : "public, only-if-cached, max-stale=2419200" ; Request request = originalRequest.newBuilder().build(); Response response = chain.proceed(request); return response.newBuilder() .removeHeader("Pragma") .removeHeader("Cache-Control") .header("Cache-Control", cacheHeaderValue) .build(); } }; } public static CompanyNameAPI provideCompanyNameAPI () { return provideRetrofit(CompanyName.getInstance().getString(R.string.base_url)).create(CompanyNameAPI.class); } }
I tried some solutions found over the internet and stackoverflow (still in the comments in the code above), because at first I thought it was a problem with rewriting "Cache-Control".
I also added READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions to the manifest, but didn't change anything.
Did I miss something? (I am testing on Samsung with Android 6.0.1 API 23)
source share