Volley的简单封装

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.xxx.xxxx.YiBanApplication;
import com.xxx.xxxx.Utils.UtilsTools;

import java.security.NoSuchAlgorithmException;
import java.util.Map;
import java.util.TreeMap;

public class VolleyClient {


    /**
     * @param requestTag    这个随便传,做为标记
     * @param urlFirst      访问地址前缀
     * @param urlLast       访问地址后缀
     * @param treeMap       要传的参数一个  TreeMap集合 (如何没有参数要上传,就上传null)
     * @param volleyRequest 传的是接口类,这个类里面必须实现两个方法,一个成功一个失败
     */
    public static void postRequestQueue(final String requestTag, String urlFirst, final String urlLast, final TreeMap treeMap, VolleyRequest volleyRequest) {
        LogUtil.e("url --- ", urlFirst + urlLast);
        if (UtilsTools.isNetWorkConnected(MyApplication.getInstance().getContext())) {
            final RequestQueue requestQueue = MyApplication.getInstance().mVollyQueue;

            removeRequest(requestQueue, requestTag);
            final StringRequest request = new StringRequest(Request.Method.POST, urlFirst + urlLast, volleyRequest.getVolleyListener(), volleyRequest.getVolleyErrorListener()) {
                @Override
                protected Map getParams() throws AuthFailureError {
                    TreeMap map = new TreeMap<>();
                    if (treeMap != null) {
                        map = treeMap;
                    }
                    // 当前版本号
                    map.put("curVersion", UtilsTools.GetVersionName(MyApplication.getInstance().getContext()));
                    return map;
                }

//                @Override
//                protected Response parseNetworkResponse(NetworkResponse response) {
//                    Map responseHeaders = response.headers;
//                    if (requestTag.equals("apiLogin")) {
//                        String cookies = responseHeaders.get("Set-Cookie");
//                        //                        LogUtil.e("----------", "cookies --- " + cookies);
//                        SharedPrefUtil.putString(MyApplication.getInstance().getContext(), "cookies", cookies);
//                    }
//                    return super.parseNetworkResponse(response);
//                }
//
//                @Override
//                public Map getHeaders() throws AuthFailureError {
//                    HashMap map = new HashMap<>();
//                    map.put("Cookie", SharedPrefUtil.getString(MyApplication.getInstance().getContext(), "cookies", ""));
//                    //                    LogUtil.e("----------", "Cookie --- " + SharedPrefUtil.getString(MyApplication.getContext(), "cookies", ""));
//                    return map;
//                }
            };
            request.setRetryPolicy(new DefaultRetryPolicy(50000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            request.setTag(requestTag);
            //        requestQueue.cancelAll(requestTag);
            requestQueue.add(request);
        } else {
            UtilsTools.MsgBox(MyApplication.getInstance().getContext(), MyApplication.getInstance().getContext().getString(R.string.network_disconnect));
        }
    }

    public static void getRequestQueue(final String requestTag, String urlFirst, final String urlLast, VolleyRequest volleyRequest) {
        LogUtil.e("url --- ", urlFirst + urlLast);
        RequestQueue requestQueue = MyApplication.getInstance().mVollyQueue;
        removeRequest(requestQueue, requestTag);
        if (UtilsTools.isNetWorkConnected(MyApplication.getInstance().getContext())) {
            StringRequest request = new StringRequest(Request.Method.GET, urlFirst + urlLast,
                    volleyRequest.getVolleyListener(), volleyRequest.getVolleyErrorListener());
            request.setTag(requestTag);
            requestQueue.add(request);
        } else {
            UtilsTools.MsgBox(MyApplication.getInstance().getContext(), MyApplication.getInstance().getContext().getString(R.string.network_disconnect));
        }
    }

    // 移除多余的请求
    private static void removeRequest(RequestQueue requestQueue, String requestTag) {
        requestQueue.cancelAll(requestTag);
    }

}

-

 

package com.xxx.xxxx.volley;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.xxx.xxxx.App.YiBanApplication;
import com.xxx.xxxx.R;
import com.xxx.xxxx.Utils.UtilsTools;

import java.util.TreeMap;

public abstract class VolleyRequest {
    /**
     * 返回的的Response
     *
     * @param s
     */
    protected abstract void onMyResponse(String s);

    /**
     * 访问错误返回
     *
     * @param volleyError
     */
    public void onMyErrorResponse(VolleyError volleyError) {
        UtilsTools.MsgBox(YiBanApplication.getContext(), YiBanApplication.getContext().getString(R.string.network_error));
    }


    /**
     * 成功
     *
     * @return
     */
    public Response.Listener getVolleyListener() {
        Response.Listener listener = new Response.Listener() {
            @Override
            public void onResponse(String s) {
                onMyResponse(s);
            }
        };
        return listener;
    }

    /**
     * 失败
     *
     * @return
     */
    public Response.ErrorListener getVolleyErrorListener() {
        Response.ErrorListener listener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                onMyErrorResponse(volleyError);
                UtilsTools.MsgBox(YiBanApplication.getContext(), YiBanApplication.getContext().getString(R.string.network_error));
            }
        };
        return listener;
    }


//       //获取Cookie的带参数post请求测试
//     Map map = new HashMap();
//     map.put("user_id", "1");
//     map.put("user_password", "1");
//
//     VolleyHelper.postRequestGetCookie(requestQueue, "ShareTo", "http://api.dev.housesaas.com/agent/user/list", map, new VolleyRequest() {
//    @Override protected void onMyResponse(String s) {
//    Log.e("noco", "" + s);
//    }
//
//    @Override protected void onMyErrorResponse(VolleyError volleyError) {
//
//    }
//    });

}

 

你可能感兴趣的:(网络框架相/请求相关,Volley,封装)