码迷,mamicode.com
首页 > 移动开发 > 详细

Android开源学习之Volley

时间:2015-04-07 12:02:04      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

在学习中注意点:

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的源码解析)



Android开源学习之Volley

标签:

原文地址:http://blog.csdn.net/zhanhong39/article/details/44917167

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!