I'm trying to get caching to work with Retrofit with no luck, and I'm not quite sure what I'm doing wrong.
Using:
build.gradle
compile 'com.squareup.okhttp:okhttp:1.5.+'
compile 'com.squareup.retrofit:retrofit:1.5.+'
Example Call
@Headers("Cache-Control: public, max-age=640000, s-maxage=640000 , max-stale=10000000")
@GET("/articles/{article}")
public void getArticle(
@Path("article") String id,
Callback<Article> cb
);
Building the API
File cacheDir = context.getCacheDir();
cache = null;
try {
cache = new HttpResponseCache(cacheDir, 10 * 1024 * 1024);
} catch (IOException e) {
e.printStackTrace();
}
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setResponseCache(cache);
OkClient okClient = new OkClient(okHttpClient);
api = new RestAdapter.Builder()
.setEndpoint(TestAPI.BASE_URL)
.setRequestInterceptor( ... only adding path params ... )
.setClient(okClient)
.build()
.create(TestAPI.class);
The call works, the only issue is actually getting it to cache... I'm checking against cache.getHitCount() after I make my calls to check if there was anything that cached, and it always turns up as 0.