Last active
September 7, 2016 12:49
-
-
Save dnldsht/8e35719147e8a7d05db9af48c76241c6 to your computer and use it in GitHub Desktop.
RequestsHelper for volley
This file contains hidden or 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 RequestsHelper { | |
private RequestQueue mRequestQueue; | |
private ImageLoader mImageLoader; | |
private static RequestsHelper mInstance; | |
private static Context ctx; | |
public static synchronized RequestsHelper getInstance(Context context) { | |
if (mInstance == null) | |
mInstance = new RequestsHelper(context); | |
return mInstance; | |
} | |
public RequestsHelper(Context context) { | |
ctx = context; | |
mRequestQueue = getRequestQueue(); | |
mImageLoader = new ImageLoader(mRequestQueue, | |
new ImageLoader.ImageCache() { | |
private final LruCache<String, Bitmap> cache = new LruCache<>(20); | |
@Override public Bitmap getBitmap(String url) { | |
return cache.get(url); | |
} | |
@Override public void putBitmap(String url, Bitmap bitmap) { | |
cache.put(url, bitmap); | |
} | |
}); | |
} | |
public void makePostRequest(String baseUrl, String id, final Map<String, String> params, | |
Response.Listener<String> responseListener, Response.ErrorListener errorListener) { | |
String url = baseUrl + id; | |
makeAndSubmitRequest(url, Request.Method.POST, params, responseListener, errorListener); | |
} | |
public void makeGetRequest(String url, final Map<String, String> params, | |
Response.Listener<String> responseListener, Response.ErrorListener errorListener) { | |
makeAndSubmitRequest(url, Request.Method.GET, params, responseListener, errorListener); | |
} | |
public void makeAndSubmitRequest(String url, int method, final Map<String, String> params, | |
Response.Listener<String> responseListener, Response.ErrorListener errorListener) { | |
submitRequest(makeRequest(url, method, params, responseListener, errorListener)); | |
} | |
public StringRequest makeRequest(String url, int method, final Map<String, String> params, | |
Response.Listener<String> responseListener, Response.ErrorListener errorListener) { | |
StringRequest stringRequest = new StringRequest(method, url, responseListener, errorListener){ | |
@Override | |
protected Map<String, String> getParams(){ return params; } | |
}; | |
stringRequest.setRetryPolicy(new DefaultRetryPolicy(0, -1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); | |
return stringRequest; | |
} | |
public void submitRequest(StringRequest request) { | |
getRequestQueue().add(request); | |
} | |
public ImageLoader getImageLoader() { | |
return mImageLoader; | |
} | |
public RequestQueue getRequestQueue() { | |
if (mRequestQueue == null) { | |
Cache cache = new DiskBasedCache(ctx.getCacheDir(), 10 * 1024 * 1024); | |
Network network = new BasicNetwork(new HurlStack()); | |
mRequestQueue = new RequestQueue(cache, network); | |
// Don't forget to start the volley request queue | |
mRequestQueue.start(); | |
} | |
return mRequestQueue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment