Google I/O 2013 – Volley: Easy, Fast Networking for Android
1、什么是volley
Volley是Ficus Kirpatrick在Gooogle I/O 2013发布的一个处理和缓存网络请求的库,能使网络通信更快,更简单,更健壮。Volley名称的由来: a burst or emission of many things or a large amount at once。在Google IO的演讲上,其配图是一幅发射火弓箭的图,有点类似流星。见下图
2、volley能做什么
volley适合小而快的数据传输。Volley应该是简化了网络通信的一些开发,特别是针对以下两种情况:
- JSON对象
- 图片加载
Volley的便利功能:
Advantages of using Volley:
- Volley automatically schedule all network requests. It means that Volley will be taking care of all the network requests your app executes for fetching response or image from web.
- Volley provides transparent disk and memory caching.
- Volley provides powerful cancellation request API. It means that you can cancel a single request or you can set blocks or scopes of requests to cancel.
- Volley provides powerful customization abilities.
- Volley provides Debugging and tracing tools
3、volley架构
Volley使用了线程池来作为基础结构,主要分为主线程,cache线程和network线程。主线程和cache线程都只有一个,而NetworkDispatcher线程可以有多个,这样能解决比并行问题。具体可以参考下图,此图节选自Google I/O 演讲。
4、使用volley
4.1 获取volley
引入Volley非常简单,首先,从git库先克隆一个下来:
git clone https://android.googlesource.com/platform/frameworks/volley
然后编译为jar包,再在自己的工程里import进来。如果git下载失败可以在这下载。 VolleyLib
4.2 JsonObjectRequest 简单文本请求
使用下面的代码实现json数据获取
mRequestQueue = Volley.newRequestQueue(this); String url = "http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json"; pd = ProgressDialog.show(this,"Please Wait...","Please Wait..."); JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i(TAG,response.toString()); parseJSON(response); va.notifyDataSetChanged(); pd.dismiss(); } },new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i(TAG,error.getMessage()); } }); mRequestQueue.add(jr);
4.3 ImageView
使用volley异步加载图像
RequestQueue mRequestQueue = Volley.newRequestQueue(this); final LruCache<String, Bitmap> mImageCache = new LruCache<String, Bitmap>( 20); ImageCache imageCache = new ImageCache() { @Override public void putBitmap(String key, Bitmap value) { mImageCache.put(key, value); } @Override public Bitmap getBitmap(String key) { return mImageCache.get(key); } }; ImageLoader mImageLoader = new ImageLoader(mRequestQueue, imageCache); // imageView是一个ImageView实例 // ImageLoader.getImageListener的第二个参数是默认的图片resource id // 第三个参数是请求失败时候的资源id,可以指定为0 ImageListener listener = ImageLoader .getImageListener(imageView, android.R.drawable.ic_menu_rotate, android.R.drawable.ic_delete); mImageLoader.get("http://a.hiphotos.baidu.com/album/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=5f024b518326cffc762ab2b2893a29e2/72f082025aafa40fa3bcf315aa64034f79f019fb.jpg", listener);
4.4 NetworkImageView
NetworkImageView是继承自ImageView,是Volley提供的一个全新的简单加载图片的控件。
NetworkImageView netImgView=(NetworkImageView)findViewById(R.id.volley_img_networkimgeview); netImgView.setTag("url"); netImgView.setImageUrl("http://a.hiphotos.baidu.com/album/h%3D800%3Bcrop%3D0%2C0%2C1280%2C800/sign=5f024b518326cffc762ab2b2893a29e2/72f082025aafa40fa3bcf315aa64034f79f019fb.jpg",mImageLoader);
/**
* @author 张兴业
参考:
Google I/O 2013 – Volley: Easy, Fast Networking for Android(ppt)