I want to try Volley combining with OkHttp but Volley cache system and OkHttp both rely on the HTTP cache as defined in the HTTP specification. So how can be disabled the cache of OkHttp for keeping one copy of HTTP cache?
EDIT: what I have done
public class VolleyUtil {
// http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
private volatile static RequestQueue sRequestQueue;
/** get the single instance of RequestQueue **/
public static RequestQueue getQueue(Context context) {
if (sRequestQueue == null) {
synchronized (VolleyUtil.class) {
if (sRequestQueue == null) {
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
client.setCache(null);
sRequestQueue = Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
VolleyLog.DEBUG = true;
}
}
}
return sRequestQueue;
}
}
Which OkHttpClient
is referenced from https://gist.github.com/bryanstern/4e8f1cb5a8e14c202750
OkHttp is a kind of HTTP client like HttpUrlConnection which implements HTTP cache, we can disable the cache of OkHttp like below:
OkHttpClient client = new OkHttpClient();
client.setCache(null);
Then, we can keep one copy of HTTP cache maintained by Volley.
IMPROVED:
I'd like to try to answer Sotti's questions.
1 I would like to know what is a good cache setup when using Volley and OkHttp.
In my project, i'm using one Volley requestQueue instance across all of restful APIs, and OkHttp worked as the transport layer for Volley like below.
public class VolleyUtil {
// http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
private volatile static RequestQueue sRequestQueue;
/** get the single instance of RequestQueue **/
public static RequestQueue getQueue(Context context) {
if (sRequestQueue == null) {
synchronized (VolleyUtil.class) {
if (sRequestQueue == null) {
OkHttpClient client = new OkHttpClient();
client.setCache(null);
sRequestQueue = Volley.newRequestQueue(context.getApplicationContext(), new OkHttpStack(client));
VolleyLog.DEBUG = true;
}
}
}
return sRequestQueue;
}}
2 Should we rely on Volley or on the OkHttp cache?
Yes, i'm using Volley cache for my HTTP Cache instead of OkHttp Cache; It works great for me.
3 What's the default behaviour out of the box?
For Volley:
it will create a "volley" default cache directory for you automatically.
/** Default on-disk cache directory. */
private static final String DEFAULT_CACHE_DIR = "volley";
public static RequestQueue newRequestQueue(Context context, HttpStack stack, int maxDiskCacheBytes) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
……
}
For OkHttp:
i can't find the default cache in the source code, and we can set the response cache like this post http://blog.denevell.org/android-okhttp-retrofit-using-cache.html
4. What's the recommended behaviour and how to achieve it?
As this post says:
Volley takes care of requesting, loading, caching, threading, synchronization and more. It’s ready to deal with JSON, images, caching, raw text and allow some customization.
I prefer to using Volley HTTP Cache because of the ease of customization.
For example, we can have much more control on the cache like this Android Volley + JSONObjectRequest Caching.
The graceful way for OkHttp
to ignore caches is:
request.setCacheControl(CacheControl.FORCE_NETWORK);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With