最近忙於課程和項目,好久沒有更新博客了。最近在項目中使用了Volley框架來請求數據,一開始接觸Volley感覺使用起來挺簡單,只需幾個簡單的操作就可以實現發送請求。以StringRequest 爲例:
//創建一個請求隊列
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "這裏是請求的鏈接...";
//創建StringRequest對象,以Post數據爲例
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("TAG", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("TAG", error.toString());
}
}) {
//填寫需要post的表單
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<>();
map.put("userId", "0"); //測試數據
map.put("sessionId", "0");
return map;
}
};
requestQueue.add(request);
以上,就是Volley請求簡單的實現。可是,在開發中,往往需要減少代碼的冗餘,並且請求隊列也應該全局共享。
本着再封裝Volley的想法,上網找了許多方案,都覺得不太適合。於是,想着怎麼設計一個方案來封裝。既然,Volley的請求隊列需要全局共享,那麼可以創建一個MyApplicantion類繼承自Application,然後在裏面共享一個請求隊列:
public class MyApplication extends Application{
private static RequestQueue queue;
private final static String QUE_TAG = "REQUEST";
/**
* 加載時新建一個請求隊列
*/
@Override
public void onCreate() {
super.onCreate();
//請求隊列在onCreate()方法裏創建,只會執行一次
queue = Volley.newRequestQueue(getApplicationContext());
}
/**
*
* @return 返回請求隊列
*/
public static RequestQueue getHttpQueue() {
return queue;
}
public static void addToQueue(StringRequest request) {
if (request != null) {
request.setTag(QUE_TAG);
queue.add(request);
}
}
/**
* 取消請求隊列,在onStop()方法中調用
*/
public static void cancelRequest() {
if (queue != null) {
queue.cancelAll(QUE_TAG);
}
}
}
有了這個請求隊列後,再設法將StringRequest這個請求封裝好,以便在需要的地方直接調用,這裏創建了一個VolleyHelper類:
/**
* volley二次封裝,用於添加和管理當前的請求
* @author qiHuang
*
*/
public class VolleyHelper {
/**
* 當前請求
*/
private StringRequest request;
public VolleyHelper() {
}
/**
* 發送請求數據
* @param url 請求的地址
* @param callBack 響應後的回調接口
*/
public void postRequest(String url, final ICallBack callBack) {
request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
callBack.onSuccess(new JSONObject(response));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
callBack.onFailed(arg0);
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return callBack.postParams();
}
};
MyApplication.addToQueue(request);
}
/**
* 取消請求
*/
public void cancelRequest() {
if (request != null) {
request.cancel();
}
}
/**
* 清空請求隊列
*/
public static void cancelAllRequest() {
MyApplication.cancelRequest();
}
}
上面涉及到一個回調接口ICallBack:
public interface ICallBack {
/**
* 成功時回調的函數
* @param object json
*/
public void onSuccess(JSONObject object);
/**
* 失敗時回調的函數
* @param error 錯誤原因
*/
public void onFailed(VolleyError error);
/**
* post參數
* @return
*/
public Map<String, String> postParams();
}
通過VolleyHelper的postRequest方法與回調接口ICallBack的結合,便可實現請求的封裝。這樣子,就可以在需要請求的地方執行下面的語句來實現請求:
VolleyHelper helper = new VolleyHelper();
helper.postRequest(URL, new ICallBack() {
@Override
public Map<String, String> postParams() {
HashMap<String, String> map = new HashMap<>(2);
map.put("useId", "0");
map.put("sessionId", "0");
return map;
}
@Override
public void onSuccess(JSONObject object) {
//成功,UI界面更新
}
@Override
public void onFailed(VolleyError error) {
//失敗,處理失敗結果
}
});
通過上面的設計,個人感覺在邏輯上比較清晰,也便於請求的添加與管理。可能還存在不妥的地方,慢慢改善吧。