Google開源庫-Volley的使用


一、什么是Volley?

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available through the open AOSP repository.  (上述的意思為:Volley是一個處理Android網絡通信的工具,它可以是的Android中的網絡通信更加的快速,高效)

                  --->url(https://developer.android.com/training/volley/index.html)

二、如何在自己的項目中引用Volley?

  image

ps:上述描述了Volley的使用方式,你需要使用git工具克隆到本地,然后使用eclipse ADT將其轉換成jar文件即可使用

三、Volley的使用講解

3.1 使用Valley實現JSON字符串請求

/**
     * 通過Volley獲取JSON數據
     */
    public void getJSONVolley(){
        RequestQueue requestQueue=Volley.newRequestQueue(this); //用於獲取一個Volley的請求對象
        String jSONDateUrl="http://www.imooc.com/api/teacher?type=4&num=30";  //請求的Url
        //(Request., url, listener, errorListener)
        //-->請求方式,請求的鏈接,成功得到請求,錯誤得到請求
        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, jSONDateUrl, null, 
                //成功得到請求的數據
                new Response.Listener<JSONObject>() {
                        public void onResponse(JSONObject response) {
                            System.out.println("response= "+response );
                        }
                    },
                    //異常得到請求的數據
              new Response.ErrorListener() {
                        public void onErrorResponse(com.android.volley.VolleyError arg0) {
                            System.out.println("對不起,有問題");
                        }
                }
            );
        requestQueue.add(jsonObjectRequest); //在請求隊列中加入當前的請求
    }

image

3.2 使用Volley異步加載圖片

 /**
     * 使用Volley異步加載圖片
      * url:http://img.mukewang.com//55237dcc0001128c06000338.jpg
     */
    public void loadImageVolley(){
        String imageUrl="http://img.mukewang.com//55237dcc0001128c06000338.jpg"; //圖片的鏈接
        RequestQueue requestQueue=Volley.newRequestQueue(this); //創建Volley的請求對象
        final LruCache<String, Bitmap> lruCache=new LruCache<String,Bitmap>(20); //創建一個緩存對象 緩存大小為20
        ImageCache imageCache=new ImageCache() {
            
            @Override
            public void putBitmap(String key, Bitmap value) {
                    lruCache.put(key, value);
            }
            
            @Override
            public Bitmap getBitmap(String key) {
                return lruCache.get(key);
            }
        };
        //使用ImageLoad進行圖片的加載,加載參數(請求,圖片的緩存)
        ImageLoader imageLoader=new ImageLoader(requestQueue, imageCache);
        
        //參數(控件名稱,找不到時候的圖片,異常時候的圖片)
        ImageListener listener=imageLoader.getImageListener(img, R.drawable.ic_launcher, R.drawable.ic_launcher);
        
        imageLoader.get(imageUrl, listener);
    }

-->使用這個方法使得我們獲取到網絡中的圖片

四、分析與總結

Volley的使用大大減少了我們異步獲取網絡數據中的代碼,使得我們更快,更高效的得到從網絡中取得數據

五、相關jar包及鏈接

5.1 Volley.jar   (http://yunpan.cn/cdYYMFt33Ky7d  訪問密碼 e9b4)

5.2 Volley官方介紹 (https://developer.android.com/training/volley/index.html)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM