Volley的使用
- 首先得到Volley的源碼,但是需要自己換時間去弄成jar包,或者使用AS的就作為依賴,所以我直接上我githup鏈接:
https://github.com/shenahobo/TestVolley 里面libs種有個jar包.直接去clone下來就好啦.
- 看這個文章你能知道那些?
- 1.Volley基本概念.
- 2.各種Request(請求)的簡單使用.
- 3.怎么取消Request(請求).
- 4.寫出請求隊列的單例模式.
- 5.設置連接的超時時間.
- 6.具體詳情請看githup.
Volley簡介
首先Volley是谷歌推出的,其底層是基于HttpUrlConnection 和 apche的httpClient ,其中sdk大于9(也就是2.2) 使用HttpUrlConnection,小于9 的
時候使用apche的HttpClient ,因為HttpUrlConnection是一個輕量級的,google對其做了許多的優化,且在android2.2 之前有一個重大的bugd調用close()函數會影響連接池,會導致鏈接復用失效.而HttpClient 則是一個成熟的框架,體量相對大一些,多用于Web,并且在android6.0去除了HttpClient.
各種Request(請求)的簡單使用.
- StringRequest請求中Get請求
-
1.先創建 RequestQueue 隊列.
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
-
2.創建StringRequest對象.
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() { @Override public void onResponse(String s) { Log.i(TAG, "ThreadName: " + Thread.currentThread().getName()); Log.i(TAG, "onResponse: s====" + s); /*Message msg = Message.obtain(); msg.what = SUCCESSFUL; msg.obj = s; mHandler.sendMessage(msg);*/ tvShow.setText(s); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Log.i(TAG, "onResponse: s====" + volleyError.getMessage()); } });
-
3.將StringRequest添加到隊列中.
mQueue.add(stringRequest);
-
- StringRequest中Post請求
同樣的步驟,只是第2步不一樣,而已.
-
1.創建stringRequest對象,重寫StringRequest中的getParams方法,在map集合中添加鍵值對.
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("params1", "value1"); map.put("params2", "value2"); return map; } };
-
2.添加到隊列中.
mQueue.add(stringRequest);
- JsonRequest的用法.
-
1.需要注意的是:JsonRequest有兩個子類,一個JsonObjectRequest, JsonArrayRequest兩個類.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, GETURL, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { tvShowTwo.setText(jsonObject.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { tvShowTwo.setText(volleyError.getMessage()); } });
-
2.添加到隊列中.
mQueue.add(stringRequest);
-
- 圖片的就不貼代碼了,需要的直接去 https://github.com/shenahobo/TestVolley
請求的取消.
- Volley請求的的取消.
-
1.取消單個,直接用請求對象調用cancle()方法.
stringRequest.cancle();
-
2.取消全部 返回true 取消全部.
mRequestQueue.cancelAll(new RequestQueue.RequestFilter() { rrturn true ;});
-
3.取消設置了TAG(標識)的請求
mRequestQueue.cancelAll(TAG);//取消tag為TAG的的請求.
-
設置請求的超時時間.
//3,設置超時時間 參數含義:超時時間 重試次數 超時時間因子
mStringRequest.setRetryPolicy(new DefaultRetryPolicy(500, 1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
請求隊列的單例模式.
public class MyApplication extends Application {
private RequestQueue mRequestQueue;
private static volatile MyApplication mInstance;
@Override
public void onCreate() {
super.onCreate();
//得到MyApplication 的對象.
mInstance = this;
if (mRequestQueue == null) {
synchronized (MyApplication.class) {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
}
}
}
public static synchronized MyApplication getInstance() {
return mInstance;
}
/**
* 得到請求隊列
*
* @return 返回請求隊列
*/
public RequestQueue getRequestQueue() {
return mRequestQueue;
}
/**
* 添加到請求隊列
*
* @param req 請求
* @param TAG 標識
* @param <T>
*/
public <T> void addRequest(Request<T> req, Object TAG) {
if (req != null) {
req.setTag(TAG);
getRequestQueue().add(req);
}
}
public <T> void add(Request<T> req) {
if (req != null) {
getRequestQueue().add(req);
}
}
/**
* 取消所有請求
*
* @param TAG 標識
* @param <T>
*/
public <T> void cancelRequest(Object TAG) {
getRequestQueue().cancelAll(TAG);
}
/**
* 取消單個請求
*
* @param req
* @param <T>
*/
public <T> void cancle(Request<T> req) {
if (req != null) {
req.cancel();
}
}
}
最后 需要注意的地方.
- 所對應的回調,通過打Log發現是在主線程中的, 所以可以直接在里面更新UI.
- 使用的時候需要添加聯網的權限.
- 創建的Applcation類需要在清單文件中進行配置.
- 取消請求的時候,Request對象還沒有創建,需要進行非空判斷,防止空指針.
- 當取消請求的時候,onResponse和onErrorResponse方法將不會執行.