Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

強大的網路通訊框架:Volley 入門教學

編輯:關於 Android 開發

強大的網路通訊框架:Volley 入門教學

Volley 是 Android 平台上相當好用的第三方開源自網路通訊框架,使用簡單、功能強大,特別適合請求量頻繁但資料量不大的場景。將下載後的 volley.jar 直接加入專案的 libs 目錄即可使用。

版面配置:activity_main.xml

版面包含一個可捲動的 TextView 與一個 ImageView,分別用來顯示文字與圖片:

 1 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 xmlns:tools="https://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.zzw.testvolley.MainActivity" > 7 8 <TextView 9 android:id="@+id/textView" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:layout_weight="1" 13 android:scrollbars="vertical" 14 android:singleLine="false" 15 android:text="@string/hello_world" /> 16 17 <ImageView 18 android:id="@+id/imageView" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:layout_weight="1" /> 22 23 </LinearLayout>

MainActivity.java

使用 Volley 的基本三步驟為:(1)取得 RequestQueue;(2)建立對應的 Request(如 StringRequest 或 ImageRequest);(3)將 Request 加入佇列。成功與失敗的結果分別在 onResponse 與 onErrorResponse 中處理。

 1 package com.zzw.testvolley; 2 3 import com.android.volley.RequestQueue; 4 import com.android.volley.Response.ErrorListener; 5 import com.android.volley.Response.Listener; 6 import com.android.volley.VolleyError; 7 import com.android.volley.toolbox.ImageRequest; 8 import com.android.volley.toolbox.StringRequest; 9 import com.android.volley.toolbox.Volley; 10 11 import android.app.Activity; 12 import android.graphics.Bitmap; 13 import android.graphics.Bitmap.Config; 14 import android.os.Bundle; 15 import android.text.method.ScrollingMovementMethod; 16 import android.view.Menu; 17 import android.view.MenuItem; 18 import android.widget.ImageView; 19 import android.widget.TextView; 20 import android.widget.Toast; 21 22 public class MainActivity extends Activity { 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.activity_main); 28 TextView textView = (TextView) findViewById(R.id.textView); 29 //設置下拉 30 textView.setMovementMethod(ScrollingMovementMethod.getInstance()); 31 32 ImageView imageView = (ImageView) findViewById(R.id.imageView); 33 34 useVolleyGetString("https://www.cnblogs.com/zzw1994", textView); 35 useVolleyGetImage("https://pic.cnblogs.com/avatar/822717/20151120000857.png", imageView); 36 37 } 38 39 private void useVolleyGetString(String url, TextView textView) { 40 41 final TextView mTextView = textView; 42 43 // 第一步,得到Volley請求 44 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 45 46 // 第二步,得到StringRequest 47 StringRequest stringRequest = new StringRequest(url, new Listener<String>() { 48 // 請求成功後返回的數據設置 49 @Override 50 public void onResponse(String response) { 51 mTextView.setText(response); 52 } 53 }, new ErrorListener() { 54 // 請求失敗後返回的數據設置 55 @Override 56 public void onErrorResponse(VolleyError error) { 57 Toast.makeText(getApplicationContext(), "加載失敗", 1).show(); 58 } 59 }); 60 61 // 第三步,添加到requestQueue 62 requestQueue.add(stringRequest); 63 } 64 65 /* 66 * 加載圖片建議使用Glide 67 * Glide:https://www.cnblogs.com/zzw1994/p/4978312.html 68 */ 69 70 private void useVolleyGetImage(String url, ImageView imageView) { 71 72 final ImageView mImageView = imageView; 73 74 // 第一步,得到Volley請求 75 RequestQueue requestQuene = Volley.newRequestQueue(getApplicationContext()); 76 77 // 第二步,得到ImageRequest 78 ImageRequest imageRequest = new ImageRequest(url, 79 // 請求成功後返回的數據設置 80 new Listener<Bitmap>() { 81 @Override 82 public void onResponse(Bitmap response) { 83 mImageView.setImageBitmap(response); 84 } 85 }, 0, 0, Config.RGB_565, new ErrorListener() { 86 // 請求失敗後返回的數據設置 87 @Override 88 public void onErrorResponse(VolleyError error) { 89 Toast.makeText(getApplicationContext(), "加載失敗", 1).show(); 90 } 91 }); 92 93 // 第三步,添加到requestQuene 94 requestQuene.add(imageRequest); 95 } 96 }

最後別忘了在 AndroidManifest.xml 中加上網路權限:

1 <uses-permission android:name="android.permission.INTERNET" />

補充:載入圖片時,目前更推薦使用 Glide 這類專門的圖片載入庫,能提供更完善的分頁與快取機制。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved