開源項目鏈接
Volley主頁:https://android.googlesource.com/platform/frameworks/volley
Volley倉庫:git clone https://android.googlesource.com/platform/frameworks/volley
Volley GitHub Demo:在GitHub主頁搜索Volley會有很多,不過建議閱讀Android Developer文檔。
背景知識
啥是Google Android Volley?
一句簡單的回答:Volley就是集AsyncHttpClient和Universal-Image-Loader優點於一身的一個Google親兒子開源框架。
Google在I/O 2013大會上發佈了Volley。它是Android平臺上的網絡通信庫,能使網絡通信更快,更簡單,更健壯。其最明顯的一個優點就是特別適合數據量不大但是通信頻繁的場景,最明顯的缺點就是大數據傳輸表現的很糟糕。
Volley提供瞭如下的便利功能:
- JSON數據和圖像等的異步下載;
- 網絡請求排序(scheduling);
- 網絡請求優先級處理;
- 緩存;
- 多級別取消請求;
- 與Activity生命週期聯動(Activity結束時同時取消所有網絡請求);
Volley對APP的版本要求是android:minSdkVersion爲8。
使用newRequestQueue和StringRequest
Volley的用法非常簡單,這裏使用官方例子發起一條HTTP GET請求,然後接收HTTP響應。首先需要獲取到一個RequestQueue對象,可以調用如下方法獲取到:
RequestQueue mQueue = Volley.newRequestQueue(context);
這裏拿到的RequestQueue是一個請求隊列對象,它可以緩存所有的HTTP請求,然後按照一定的算法併發地發出這些請求。
RequestQueue內部的設計就是非常合適高併發的,因此我們不必爲每一次HTTP請求都創建一個RequestQueue對象,這是非常浪費資源的,基本上在每一個需要和網絡交互的Activity中創建一個RequestQueue對象就足夠了。
StringRequest的構造函數需要傳入四個參數(有一個三個參數的構造函數,默認是GET方式),第一個參數就是目標服務器的URL地址,第二個參數是服務器響應成功的回調,第三個參數是服務器響應失敗的回調。將這個StringRequest對象添加到RequestQueue裏面就可以了。使用Volley時您可以從任何線程開始請求,但響應始終傳遞到了主線程上。
final TextView mTextView = (TextView) findViewById(R.id.text);
...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
如上代碼就示範了一個簡單的HTTP GET請求。
既然HTTP GET的例子這麼easy搞定,是不是該再整一個POST的例子呢?
POST有點曲線救國,因爲StringRequest中並沒有提供設置POST參數的方法,但是當發出POST請求的時候,Volley會嘗試調用StringRequest父類Request的getParams()方法來獲取POST參數,所以我們只需要重寫StringRequest的getParams()方法,在這裏設置POST參數就可以了,代碼如下所示:
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Method.POST, url, listener, errorListener) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
return map;
}
};
queue.add(stringRequest);
如上代碼就示範了一個簡單的HTTP POST請求。
使用newRequestQueue和JsonRequest
和StringRequest基本上差不多,JsonRequest也是繼承自Request類的,不過JsonRequest是一個抽象類,我們無法直接創建它的實例。JsonRequest有兩個直接的子類,JsonObjectRequest和JsonArrayRequest。所以對於JSON格式的數據基本沒啥壓力了,他都幫你搞完了,如下展示一個簡單例子:
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("http://json_test.php", null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});
queue.add(jsonObjectRequest);
如上就是一個Volley的JSON HTTP的簡單例子咯!
取消Request
要取消一個請求,調用cancel()即可。一旦取消,Volley保證你的響應處理程序將永遠不會被調用。這意味着在實踐中,你可以取消所有待定的請求在你Activity的onStop()方法中,你不必亂拋垃圾的響應處理程序或者檢查getActivity() == NULL,無論onSaveInstanceState()是否已經被調用。
你發出去的請求必須要自己保證是可控的,在這裏還有一個更簡單的方法:你可以標記發送的每個請求對象,然後你可以使用這個標籤來提供請求取消的範圍。
這裏是一個使用字符串值標籤的例子:
public static final String TAG = "MyTag";
StringRequest stringRequest; // Assume this exists.
RequestQueue mRequestQueue; // Assume this exists.
// Set the tag on the request.
stringRequest.setTag(TAG);
// Add the request to the RequestQueue.
mRequestQueue.add(stringRequest);
@Override
protected void onStop () {
super.onStop();
if (mRequestQueue != null) {
mRequestQueue.cancelAll(TAG);
}
}
現在是不是發現Volley灰常簡單了呢!就是舉一反三的操作。
好了,不扯了,網絡HTTP的Volley簡單使用先說到這裏。
使用newRequestQueue和ImageRequest
ImageRequest也是繼承自Request的,因此它的用法和上面的也是基本相同的。
ImageRequest imageRequest = new ImageRequest(
"http://developer.android.com/images/home/aw_dac.png",
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
imageView.setImageBitmap(response);
}
}, 0, 0, Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
imageView.setImageResource(R.drawable.default_image);
}
});
ImageRequest的構造函數有六個參數,第一個參數是圖片URL;第二個參數是圖片請求成功的回調;第三第四個參數分別用於指定允許圖片最大的寬度和高度,如果指定的網絡圖片的寬度或高度大於這裏的最大值,則會對圖片進行壓縮,指定成0的話就表示不管圖片有多大,都不會進行壓縮;第五個參數用於指定圖片深度;第六個參數是圖片請求失敗的回調。
使用newRequestQueue和ImageLoader
ImageLoader也可以用於加載網絡上的圖片,並且它的內部也是使用ImageRequest來實現的,不過ImageLoader明顯要比ImageRequest更加高效,因爲它不僅可以幫我們對圖片進行緩存,還可以過濾掉重複的鏈接,避免重複發送請求。
由於ImageLoader已經不是繼承自Request,所以它的用法變爲如下:
- 創建一個RequestQueue對象。
- 創建一個ImageLoader對象。
- 獲取一個ImageListener對象。
- 調用ImageLoader的get()方法加載網絡上的圖片。
RequestQueue mQueue = Volley.newRequestQueue(context);
ImageLoader imageLoader = new ImageLoader(mQueue, new ImageCache() {
@Override
public void putBitmap(String url, Bitmap bitmap) {
//未實現
}
@Override
public Bitmap getBitmap(String url) {
//未實現
return null;
}
});
ImageListener listener = ImageLoader.getImageListener(imageView,
R.drawable.default_image, R.drawable.failed_image);
imageLoader.get("http://google/pictures/1201.jpeg", listener, 200, 200);
如上簡單使用了ImageLoader進行圖片加載。
現在進行升級,實現ImageCache接口如下:
public class BitmapCache implements ImageCache {
private LruCache<String, Bitmap> mCache;
public BitmapCache() {
int maxSize = 5 * 1024 * 1024; //演示寫死,實際需要動態計算
mCache = new LruCache<String, Bitmap>(maxSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}
};
}
@Override
public Bitmap getBitmap(String url) {
return mCache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
mCache.put(url, bitmap);
}
}
這裏這麼搞一下就簡單的把ImageLoader優勢展示了一把。
使用newRequestQueue和NetworkImageView
NetworkImageView是一個繼承自ImageView的View控件,擁有ImageView控件的所有功能,並且在ImageView的基礎上加入了加載網絡圖片的功能。NetworkImageView控件的用法分爲以下五步:
- 創建一個RequestQueue對象。
- 創建一個ImageLoader對象。
- 在佈局文件中添加一個NetworkImageView控件。
- 在代碼中獲取該控件的實例。
- 設置要加載的圖片地址。
這裏以XML文件結合java的方式演示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/network_image_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
networkImageView = (NetworkImageView) findViewById(R.id.network_image_view);
networkImageView.setDefaultImageResId(R.drawable.default_image);
networkImageView.setErrorImageResId(R.drawable.failed_image);
networkImageView.setImageUrl("https://img-my.csdn.net/uploads/201404/13/1397393290_5765.jpeg", imageLoader);
如上演示了Volley的一些基本使用特性和方法。
Volley請求管理方式
在Android Developer上看到的這幅圖:
RequestQueue會維護一個緩存調度線程(cache線程)和一個網絡調度線程池(net線程),當一個Request被加到隊列中的時候,cache線程會把這個請求進行篩選:如果這個請求的內容可以在緩存中找到,cache線程會親自解析相應內容,並分發到主線程(UI)。如果緩存中沒有,這個request就會被加入到另一個NetworkQueue,所有真正準備進行網絡通信的request都在這裏,第一個可用的net線程會從NetworkQueue中拿出一個request扔向服務器。當響應數據到的時候,這個net線程會解析原始響應數據,寫入緩存,並把解析後的結果返回給主線程。
文章轉自:http://blog.csdn.net/yanbober/article/details/45307015
感謝博主的無私分享!