Android Volley架構的使用(三),androidvolley

來源:互聯網
上載者:User

Android Volley架構的使用(三),androidvolley

Volley架構的學習馬上就可以“殺青”了,哈哈,有木有點小激動呢,之所以將這個架構分成三篇來寫,而且涉及的細節比較多,是因為考慮到後面還要學習幾個Android http架構,只要認認真真看完volley架構的學習,後面幾個架構的學習簡直“易如反掌”。在開始這篇文章之前
建議先看前兩篇文章:

Android Volley的使用(一)

Android Volley的使用(二)

這篇主要介紹圖片請求以及緩衝的問題,這在開發中似乎是最常用到的,所以最後咱們一起來做個Demo來練習一下!

文章結構如下:

Image Request

為了更方便的使用Volley中的圖片請求,我們同樣先在ApplicationController類中封裝一個ImageLoader

public class ApplicationController extends Application{        /*前面寫過的方法不再贅述*/        private ImageLoader imageLoader;        ....        public ImageLoader getImageLoader(){            getRequestQueue();            //如果imageLoader為空白則建立它,第二個參數代表處理映像緩衝的類            if(imageLoader==null){                imageLoader=new ImagerLoader(this.reqQueue,new LruBitmapCache());            }            return this.imageLoader;        }    }    public class LruBitmapCache extends LruCache<String,Bitmap> implements ImageCache{        public static int getDefaultLruCacheSize(){            final int maxMemory=(int)(Runtime.getRuntime().maxMemory/1024);            final int cacheSize=maxMemory/8;            return cacheSize;        }        public LruBitmapCache(){            this(getDefaultLruBitmapCacheSize);        }        public LruBitmapCache(int sizeInKiloBytes){             super(sizeInkiloBytes);        }        @Override        public int sizeOf(String key,Bitmap Value){            return value.getRowBytes()*value.getHeight()/1024;        }        @Override        public Bitmap getBitmap(String url){            return get(url);        }        @Override        public void putBitmap(String url,Bitmap bitmap){            put(url,bitmap);        }    }
 ImageLoader imageLoader=ApplicationController.getInstance().getImageLoader();

可以使用Volley自己提供的一個Image視圖,NetworkImageView,幾行代碼就可以搞定

//將NetworkImageView布局在布局檔案中NetworkImageView imageView=(NetworkImageView)findViewById(R.id.networkimageview); //需要用到imageLoader imageView.setImageUrl(url,imageLoader);

如果要將圖片直接載入ImageView,可以通過以下方法:

ImageLoader imageLoader=ApplicationController.getInstance().getImageLoader();imageLoader.get(url,new ImageListener(){   @Override   public void onResponse(ImageContainer response,boolean arg)  {        if(response.getBitmap()!=null){        //設定imageView        imageView.setImageBitmap(response.getBitmap());        }    }    @Override    public void onErrorResponse(VolleyError error){        Log.e(TAG,"Image Error"+error.getMessage());        }    });
Volley Cache

Volley有著強大的緩衝機制用來維護請求到的緩衝,這節省了不必要的網路消耗和等待時間,下面是一些關於緩衝的常用方法

Cache cache=ApplicationController.getRequestQueue().getCache();        Entry entry=cache.get(url);        if(entry!=null){            try{                String data=new String(entry.data,"Utf-8");                //處理data,將其轉化為JSON,XML,Bitmap等等                }catch(UnspportedEncodingException e){                    e.printStackTrace();                }        }else{            //緩衝中不存在,做網路請求        }
ApplicationController.getInstance().getRequestQueue().getCache().invalidate(url,true);
StringRequest req=new StringRequest(...);req.setShouldCache(false);
ApplicationController.getInstance().getRequestQueue().getCache().remove(url);
ApplicationController.getInstance().getRequestQueue().getCache().clear(url);
通過Volley擷取資料填充自訂ListView限於篇幅的問題放到下篇文章中來寫->_<總結:

綜上,已經學完了Volley架構的使用,在實際應用中遇到具體的問題需要具體考慮,必要時要學會查閱資料,除了以上幾篇提到的參考資料,最好能翻牆去看看google官方關於Volley的文檔,我想基本上能解決我們所碰到的問題。

參考資料:Android working with Volley Library

  • 微博: @明桑Android黑曆史
  • 郵箱: <13141459344@163.com>
  • 個人首頁: 明桑戰勝Android汪的黑曆史

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.