Android開發之Volley網路通訊架構,androidvolley

來源:互聯網
上載者:User

Android開發之Volley網路通訊架構,androidvolley

今天用了一下Volley網路通訊架構,感覺挺好用的,寫個部落格記錄一下使用方法,方便以後VC。


Volley(Google提供的網路通訊庫,能使網路通訊更快,更簡單,更健壯。)

功能模組:


1. JSON,映像等的非同步下載
---------------------------------------------------------------------------------------
2. 網路請求的排序(scheduling)
---------------------------------------------------------------------------------------
3. 網路請求的優先順序處理
---------------------------------------------------------------------------------------
4. 緩衝
---------------------------------------------------------------------------------------
5. 多層級取消請求
---------------------------------------------------------------------------------------
6. 和Activity和生命週期的聯動(Activity結束時同時取消所有網路請求)




優點:Volley特別適合資料量不大但是通訊頻繁的情境。


Volley的jar包CSDN上有很多,也可以上GitHub上下載。


下面是我寫的一個簡單的例子。


import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v4.util.LruCache; import android.widget.ImageView; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.ImageLoader.ImageCache; import com.android.volley.toolbox.ImageLoader.ImageListener; import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.NetworkImageView; import com.android.volley.toolbox.Volley;    /** * Demo描述:  *   利用Volley擷取JSON資料 *   利用Volley非同步載入圖片 *   利用NetworkImageView載入網路圖片 */ public class MainActivity extends Activity {     private ImageView mImageView;     private NetworkImageView mNetworkImageView;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         init();     }            private void init(){         mImageView=(ImageView) findViewById(R.id.imageView);         mNetworkImageView=(NetworkImageView)findViewById(R.id.networkImageView);         getJSONByVolley();         loadImageByVolley();         showImageByNetworkImageView();     }        /**     * 利用Volley擷取JSON資料     */     private void getJSONByVolley() {         RequestQueue requestQueue = Volley.newRequestQueue(this);         String JSONDataUrl = "要訪問的URL";         final ProgressDialog progressDialog = ProgressDialog.show(this, "This is title", "...Loading...");            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(                 Request.Method.GET,                  JSONDataUrl,                  null,                 new Response.Listener<JSONObject>() {                     @Override                     public void onResponse(JSONObject response) {                         System.out.println("response="+response);                         if (progressDialog.isShowing()&&progressDialog!=null) {                             progressDialog.dismiss();                         }                     }                 },                  new Response.ErrorListener() {                     @Override                     public void onErrorResponse(VolleyError arg0) {                            System.out.println("sorry,Error");                     }                 });         requestQueue.add(jsonObjectRequest);     }                   /**     * 利用Volley非同步載入圖片     *      * getImageListener(ImageView view, int defaultImageResId, int errorImageResId)     * 第一個參數:顯示圖片的ImageView     * 第二個參數:預設顯示的圖片資源     * 第三個參數:載入錯誤時顯示的圖片資源     */     private void loadImageByVolley(){         String imageUrl="要訪問的URL";         RequestQueue requestQueue = Volley.newRequestQueue(this);         final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(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);             }         };         ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);         ImageListener listener = ImageLoader.getImageListener(mImageView, R.drawable.ic_launcher,R.drawable.ic_launcher);         imageLoader.get(imageUrl, listener);     }            /**     * 利用NetworkImageView顯示網狀圖片     */     private void showImageByNetworkImageView(){         String imageUrl="要訪問的URL";         RequestQueue requestQueue = Volley.newRequestQueue(this);         final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(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);             }         };         ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);         mNetworkImageView.setImageUrl(imageUrl,imageLoader);     }    }



布局檔案:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"      >        <com.android.volley.toolbox.NetworkImageView         android:id="@+id/networkImageView"         android:layout_width="120dip"         android:layout_height="120dip"     />             <ImageView         android:id="@+id/imageView"         android:layout_width="120dip"         android:layout_height="120dip"     />    </RelativeLayout>



記錄完畢!下次用到直接VC!

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

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