Skip to content

Instantly share code, notes, and snippets.

@zaidhoona
Last active May 26, 2019 14:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zaidhoona/af33539d3b8ede09353b to your computer and use it in GitHub Desktop.
Save zaidhoona/af33539d3b8ede09353b to your computer and use it in GitHub Desktop.
Volley Serial Request Queue
public class SerialQueue {
private static final int MAX_CACHE_SIZE = 2097152; //2 MB
private static final int MAX_SERIAL_THREAD_POOL_SIZE = 1;
private static final int MAX_MULTI_THREAD_POOL_SIZE = 4;
private static RequestQueue serialRequestQueue;
/**
* Use to fetch the serial request queue
*/
public static RequestQueue getSerialRequestQueue(Context context) {
if (serialRequestQueue == null) {
serialRequestQueue = prepareSerialRequestQueue(context);
serialRequestQueue.start();
}
return serialRequestQueue;
}
private static RequestQueue prepareSerialRequestQueue(Context context) {
Cache cache = new DiskBasedCache(context.getCacheDir(), MAX_CACHE_SIZE);
Network network = getNetwork();
return new RequestQueue(cache, network, MAX_SERIAL_THREAD_POOL_SIZE);
}
private static Network getNetwork() {
HttpStack stack;
String userAgent = "volley/0";
if(Build.VERSION.SDK_INT >= 9) {
stack = new HurlStack();
} else {
stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
}
return new BasicNetwork(stack);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment