zoukankan      html  css  js  c++  java
  • volley

    //关于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;
        }
    }
  • 相关阅读:
    hdu 1290 献给杭电五十周年校庆的礼物 (DP)
    hdu 3123 GCC (数学)
    hdu 1207 汉诺塔II (DP)
    hdu 1267 下沙的沙子有几粒? (DP)
    hdu 1249 三角形 (DP)
    hdu 2132 An easy problem (递推)
    hdu 2139 Calculate the formula (递推)
    hdu 1284 钱币兑换问题 (DP)
    hdu 4151 The Special Number (DP)
    hdu 1143 Tri Tiling (DP)
  • 原文地址:https://www.cnblogs.com/yzjT-mac/p/5818301.html
Copyright © 2011-2022 走看看