IMO, if your project uses Google's volley as a module (not jar file), you can customize its classes like the following:
OPTION #1:
First file, RequestQueue.java:
add a class variable private boolean mCacheUsed = true;
and the following constructors:
public RequestQueue(Cache cache, Network network, int threadPoolSize,
ResponseDelivery delivery, boolean cacheUsed) {
mCache = cache;
mNetwork = network;
mDispatchers = new NetworkDispatcher[threadPoolSize];
mDelivery = delivery;
mCacheUsed = cacheUsed;
}
public RequestQueue(Cache cache, Network network, int threadPoolSize, boolean cacheUsed) {
this(cache, network, threadPoolSize,
new ExecutorDelivery(new Handler(Looper.getMainLooper())), cacheUsed);
}
public RequestQueue(Cache cache, Network network, boolean cacheUsed) {
this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE, cacheUsed);
}
then, inside public <T> Request<T> add(Request<T> request) {
, you check as the following:
// If the request is uncacheable, skip the cache queue and go straight to the network.
if (!request.shouldCache() || !mCacheUsed) {
mNetworkQueue.add(request);
return request;
}
Second file, Volley.java:
public static RequestQueue newRequestQueue(Context context, HttpStack stack, boolean cacheUsed) {
File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
String userAgent = "volley/0";
try {
String packageName = context.getPackageName();
PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
userAgent = packageName + "/" + info.versionCode;
} catch (NameNotFoundException e) {
}
if (stack == null) {
if (Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
// Prior to Gingerbread, HttpUrlConnection was unreliable.
// See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
}
Network network = new BasicNetwork(stack);
RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network, cacheUsed);
queue.start();
return queue;
}
public static RequestQueue newRequestQueue(Context context, boolean cacheUsed) {
return newRequestQueue(context, null, cacheUsed);
}
Finally, in MainActivity, for example:
If want to use available cache:
RequestQueue requestQueue = Volley.newRequestQueue(this, true);
If don't want to use available cache:
RequestQueue requestQueue = Volley.newRequestQueue(this, false);
OPTION #2:
Request.java:
Add a class variable public boolean mSkipAvailableCache = false;
RequestQueue.java:
inside public <T> Request<T> add(Request<T> request)
, you check as the following:
// If the request is uncacheable, skip the cache queue and go straight to the network.
if (!request.shouldCache() || request.mSkipAvailableCache) {
mNetworkQueue.add(request);
return request;
}
MainActivity.java:
You can set
jsonArrayRequest.mSkipAvailableCache = true;
available cache will not be used.
Hope this helps!