參考:html
Android網絡通訊庫Volley簡介java
Google I/O 2013 – Volley: Easy, Fast Networking for Android(ppt)android
一、什麼是volley
Volley是Ficus Kirpatrick在Gooogle I/O 2013發佈的一個處理和緩存網絡請求的庫,能使網絡通訊更快,更簡單,更健壯。Volley名稱的由來: a burst or emission of many things or a large amount at once。在Google IO的演講上,其配圖是一幅發射火弓箭的圖,有點相似流星。見下圖
二、volley能作什麼
volley適合小而快的數據傳輸。Volley應該是簡化了網絡通訊的一些開發,特別是針對如下兩種狀況:
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
三、volley架構
Volley使用了線程池來做爲基礎結構,主要分爲主線程,cache線程和network線程。主線程和cache線程都只有一個,而NetworkDispatcher線程能夠有多個,這樣能解決比並行問題。具體能夠參考下圖,此圖節選自Google I/O 演講。git
四、使用volley
4.1 獲取volley
引入Volley很是簡單,首先,從git庫先克隆一個下來:web
- git clone https://android.googlesource.com/platform/frameworks/volley
而後編譯爲jar包,再在本身的工程裏import進來。若是git下載失敗能夠在這下載。 VolleyLibjson
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);
-
-
-
- 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提供的一個全新的簡單加載圖片的控件。api
- 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);