Skip to content

Instantly share code, notes, and snippets.

@vjdhama
Created March 1, 2014 13:57
Show Gist options
  • Save vjdhama/9290080 to your computer and use it in GitHub Desktop.
Save vjdhama/9290080 to your computer and use it in GitHub Desktop.
Volley Singleton
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class VolleyRequest {
private static VolleyRequest mInstance = null;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private VolleyRequest(Context context){
mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
});
}
public static VolleyRequest getInstance(Context context){
if(mInstance == null){
mInstance = new VolleyRequest(context);
}
return mInstance;
}
public RequestQueue getRequestQueue(){
return this.mRequestQueue;
}
public ImageLoader getImageLoader(){
return this.mImageLoader;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment