标签:
一、
//关于volley的使用,谷歌方面推荐使用单例模式
代码如下:
import android.content.Context; import android.graphics.Bitmap; import android.util.LruCache; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.Volley; /** * Created by YZJ on 2016/8/26. */ public class VolleySingleton { private static VolleySingleton volleySingleton; private RequestQueue mRequestQueue; private ImageLoader mImageLoader; private Context mContext; public VolleySingleton(Context context) { this.mContext = context; mRequestQueue = getRequestQueue(); mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache(){ private final LruCache<String,Bitmap> cache = new LruCache<String ,Bitmap>(20); @Override public Bitmap getBitmap(String url){ return cache.get(url); } @Override public void putBitmap(String url,Bitmap bitmap){ cache.put(url,bitmap); } }); } public static synchronized VolleySingleton getVolleySingleton(Context context){ if(volleySingleton == null){ volleySingleton = new VolleySingleton(context); } return volleySingleton; } public RequestQueue getRequestQueue(){ if(mRequestQueue == null){ mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext()); } return mRequestQueue; } public <T> void addToRequestQueue(Request<T> req){ getRequestQueue().add(req); } public ImageLoader getImageLoader() { return mImageLoader; } }
标签:
原文地址:http://www.cnblogs.com/yzjT-mac/p/5862803.html