对Volley的简单的二次回调封装
1、新建一个VolleyInterface类
public abstract class VolleyInterface {
public Context mContext;
public static Response.Listener mListener;
public static Response.ErrorListener mErrorListener;
public VolleyInterface(Context context, Response.Listener listener, Response.ErrorListener errorListener){
this.mContext = context;
this.mListener = listener;
this.mErrorListener = errorListener;
}
public abstract void onMySuccess(String result);
public abstract void onMyError(VolleyError error);
public Response.Listener loadingListener(){
mListener = new Response.Listener() {
@Override
public void onResponse(String s) {
//弹出加载对话框
onMySuccess(s);
}
};
return mListener;
}
public Response.ErrorListener errorListener(){
mErrorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
onMyError(volleyError);
//提示请求失败
String msg = "";
if (volleyError instanceof TimeoutError){
msg = MyApplication.netWorkTimeOut;
}
// else if (volleyError != null && volleyError.getMessage() != null){
// try {
// msg = new JSONObject(volleyError.getMessage().optString("message"));
// } catch (JSONException e) {
// e.printStackTrace();
// }
// }
else {
msg = MyApplication.netWorkError;
}
Toast.makeText(mContext,msg,Toast.LENGTH_LONG).show();
Log.e("error msg","url = error msg" + msg);
}
};
return mErrorListener;
}
}
2、建一个VolleyRequest 类
public class VolleyRequest {
public static StringRequest stringRequest;
public static Context mContext;
public static void RequestGet(Context mContext,String url,String tag,VolleyInterface vif){
//先把带tag标签的请求关闭掉,避免重复的请求来小号内存
MyApplication.getHttpQueues().cancelAll(tag);
stringRequest = new StringRequest(Request.Method.GET,url,vif.loadingListener(),vif.errorListener());
stringRequest.setTag(tag);
MyApplication.getHttpQueues().add(stringRequest);
MyApplication.getHttpQueues().start();
}
public static void RequestPost(Context mContext, String url, String tag, final Map params, VolleyInterface vif){
MyApplication.getHttpQueues().cancelAll(tag);
stringRequest = new StringRequest(url,vif.loadingListener(),vif.errorListener()){
@Override
protected Map getParams() throws AuthFailureError {
return params;
}
};
stringRequest.setTag(tag);
MyApplication.getHttpQueues().add(stringRequest);
MyApplication.getHttpQueues().start();
}
}
3、使用Volley简单封装后的get请求:
//使用封装后的Get请求
private void request_Get(){
String url = "http://httpbin.org/get?site=code&network=tutsplus";
VolleyRequest.RequestGet(this, url, "abcGet", new VolleyInterface(this,VolleyInterface.mListener,VolleyInterface.mErrorListener) {
@Override
public void onMySuccess(String result) {
tv_content.setText("请求结果:\n" + result);
}
@Override
public void onMyError(VolleyError error) {
tv_content.setText("请求结果:\n" + error.toString());
}
});
}
4、附加一点Volley与Activity生命周期的联动
只需在Activity生命周期的onStop()方法里面把之前请求中所做的tag标记的请求关闭掉即可
@Override
protected void onStop() {
super.onStop();
MyApplication.getHttpQueues().cancelAll("abcGet");
}
单独的get与post请求请参考上一篇文章Volley的get和post请求方式的使用
http://blog.csdn.net/u013184970/article/details/70036566