Volley架構的介紹使用,volley架構介紹

來源:互聯網
上載者:User

Volley架構的介紹使用,volley架構介紹

  Volley是在2013年的Google I/O 2013大會上發布的,是我們的網路通訊更快,更簡單,更方便。對於初學者來講是一個很好的架構。

  簡單來說,它提供了如下的便利功能:

 
  • JSON,映像等的非同步下載;
  • 網路請求的排序(scheduling)
  • 網路請求的優先順序處理
  • 緩衝
  • 多層級取消請求
  • 和Activity和生命週期的聯動(Activity結束時同時取消所有網路請求)
一、擷取JSON對象

   1.1聲明RequestQueue

  聲明一個新的RequestQueue對象

private RequestQueue mRequestQueue; 

  在onCreate初始化mRequestQueue

mRequestQueue =  Volley.newRequestQueue(this);  

  聲明並使用Request

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);  

  Volley提供了JsonObjectRequest、JsonArrayRequest、StringRequest等Request形式。

JsonObjectRequest:返回JSON對象。

JsonArrayRequest:返回JsonArray。

StringRequest:返回String,這樣可以自己處理資料,更加靈活。

二、載入網狀圖片
private Button myBtn;    private ImageView myImageView;    private ImageLoader imageLoader;    private RequestQueue requestQueue;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_image_loader);        requestQueue= Volley.newRequestQueue(getApplicationContext());//請求隊列        imageLoader=new ImageLoader(requestQueue, new BitmapCache());        myBtn= (Button) findViewById(R.id.myImageLoaderBtn);        myImageView=(ImageView)findViewById(R.id.myImageLoader);        myBtn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                ImageLoader.ImageListener listener = ImageLoader.getImageListener(myImageView,R.drawable.ic_launcher, R.drawable.ic_launcher);//後兩個參數分別是載入的預設圖和載入出錯後要顯示的圖片                imageLoader.get("http://192.168.191.1:8080/JerehEdu/image/logo.jpg", listener,200,200);            }        });}

  在載入圖片的時候使用到了BitmapCache這樣一個類,這個類是我們繼承了Volley中的ImageCache實現的,它能夠對我們載入的圖片進行一個緩衝。

public class BitmapCache implements ImageCache {    private LruCache<String, Bitmap> mCache;    public BitmapCache() {        int maxSize = 10 * 1024 * 1024;        mCache = new LruCache<String, Bitmap>(maxSize) {            @Override            protected int sizeOf(String key, Bitmap value) {                // TODO Auto-generated method stub                return value.getRowBytes() * value.getHeight();            }        };    }    @Override    public Bitmap getBitmap(String arg0) {        // TODO Auto-generated method stub        return mCache.get(arg0);    }    @Override    public void putBitmap(String arg0, Bitmap arg1) {        // TODO Auto-generated method stub        mCache.put(arg0, arg1);    }}

  現在應用到的網路通訊架構有很多像如Volley這樣有很多,最近也在研究xutils,感覺這個也很不錯,也希望和大家交流經驗。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.