Android开源学习之Volley

在学习中注意点:

1、要在AndroidManifest.xml中加入上网允许权限

<uses-permission android:name="android.permission.INTERNET"/>

2、适合使用单例设计模式,减少重复代码

MySingleton.java

public class MySingleton {

	private static MySingleton mInstance;
	private RequestQueue mRequestQueue;
	private ImageLoader mImageLoader;
	private static Context mCtx;
	
	@SuppressLint("NewApi")
	private MySingleton(Context context){
		mCtx = context;
		mRequestQueue = getRequestQueue();
		mImageLoader = new ImageLoader(mRequestQueue,new ImageCache() {
			private final LruCache<String,Bitmap>
					cache = new LruCache<String,Bitmap>(20);
			@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
			@SuppressLint("NewApi")
			@Override
			public void putBitmap(String url, Bitmap bitmap) {
				cache.put(url, bitmap);
			}
			
			@Override
			public Bitmap getBitmap(String url) {
				return cache.get(url);
			}
		});
	}
	public static synchronized MySingleton getInstance(Context context){
		if(mInstance == null){
			mInstance = new MySingleton(context);
		}
		return mInstance;
	}
	public RequestQueue getRequestQueue(){
		if(mRequestQueue == null){
			 // getApplicationContext() is key, it keeps you from leaking the
            // Activity or BroadcastReceiver if someone passes one in.
			mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
		}
		return mRequestQueue;
	}
	public <T> void addToRequestQueue(Request<T> req){
		getRequestQueue().add(req);
	}
	public ImageLoader getImageLoader(){
		return mImageLoader;
	}
	
}


官方学习网站:

http://developer.android.com/training/volley(经常打不开)

http://www.cnblogs.com/bvin/p/4350731.html(博主的官方摘录)

分享总结:

http://blog.csdn.net/guolin_blog/article/details/17482095(Volley完全解析一~四)

http://blog.csdn.net/chenfuduo_loveit/article/details/41773607(博主的译文)

http://codekk.com/open-source-project-analysis/detail/Android/grumoon/Volley%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90(codeKK的源码解析)



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。