Last active
May 26, 2019 14:30
-
-
Save zaidhoona/af33539d3b8ede09353b to your computer and use it in GitHub Desktop.
Volley Serial Request Queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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