Volley的简单的二次回调封装

来源:互联网 发布:pro recorder软件下载 编辑:程序博客网 时间:2024/06/06 12:49
对Volley的简单的二次回调封装

1、新建一个VolleyInterface类

public abstract class VolleyInterface {    public Context mContext;    public static Response.Listener<String> mListener;    public static Response.ErrorListener mErrorListener;    public VolleyInterface(Context context, Response.Listener<String> 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<String> loadingListener(){        mListener = new Response.Listener<String>() {            @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<String,String> params, VolleyInterface vif){        MyApplication.getHttpQueues().cancelAll(tag);        stringRequest = new StringRequest(url,vif.loadingListener(),vif.errorListener()){            @Override            protected Map<String, String> 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、附加一点VolleyActivity生命周期的联动

只需在Activity生命周期的onStop()方法里面把之前请求中所做的tag标记的请求关闭掉即可
@Overrideprotected void onStop() {    super.onStop();    MyApplication.getHttpQueues().cancelAll("abcGet");}

单独的get与post请求请参考上一篇文章Volley的get和post请求方式的使用
http://blog.csdn.net/u013184970/article/details/70036566
0 0
原创粉丝点击