简单封装volley 实现快速调用

首先要依赖volley的库,网上有很多库,基本都可以使用,我选择的是

compile 'com.dubsmash.volley:library:2.0.1'

基本思路:将网络请求封装成一个工具类,需要使用的时候调用,通过接口将返回的数据传递出去

首先先写一个接口吧

public interface Interface_volley_respose {
    public void onSuccesses(String respose);//用来返回请求来的数据
    public  void onError(int error);
}

 

接着创建一个工具类

public class Volley_Utils {
//    DEPRECATED_GET_OR_POST = -1;
//    GET = 0;
//    POST = 1;
//    PUT = 2;
//    DELETE = 3;
//    HEAD = 4;
//    OPTIONS = 5;
//    TRACE = 6;
//    PATCH = 7;

    Interface_volley_respose mInterface;//通过有参构造方法实例化接口

    RequestQueue mQueue;
    StringRequest mStringRequest;




    public Volley_Utils(Interface_volley_respose mInterface) {
        this.mInterface = mInterface;
    }

    public void Http(String URL, final Context mContext, int Method) {
        mQueue = Volley.newRequestQueue(mContext);
        mStringRequest = new StringRequest(Method, URL, new Response.Listener() {
            @Override
            public void onResponse(String response) {//这里response 是请求返回的数据
                if (response != null || response.length() != 0) {
                   
                   mInterface.onSuccesses(response);
                    
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error.networkResponse != null) {
                    int code = error.networkResponse.statusCode;
                    mInterface.onError(code);
                }

            }
        }) {//重写方法 解决json返回乱码
            protected Response  parseNetworkResponse(NetworkResponse response)
            {
                String string;
                try {
                    string = new String(response.data,"UTF-8");
                    return Response.success(string, HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                    return Response.error(new ParseError(e));
                }
        }
        };

        mQueue.add(mStringRequest);

    }

    public void postHttp(String URL, final Context mContext, int Method, final Map map) {
        mQueue = Volley.newRequestQueue(mContext);

        mStringRequest = new StringRequest(Method, URL, new Response.Listener() {
            @Override
            public void onResponse(String response) {
                if (response != null || response.length() != 0) {

                        mInterface.onSuccesses(response);
                
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error.networkResponse != null) {
                    int code = error.networkResponse.statusCode;
                    mInterface.onError(code);
                }

            }
        }) {
            @Override
            protected Map getParams() throws AuthFailureError {

                return map;
            }
        };
        mStringRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));
        mQueue.add(mStringRequest);

    }


    public void volleyCancle() {//取消网络请求
        mStringRequest.cancel();
    }

}

最后直接调用 就可以 

举例:

 new Volley_Utils(new Interface_volley_respose() {
            @Override
            public void onSuccesses(String respose) {

               

            }

            @Override
            public void onError(int error) {

            }
        }).postHttp(Urls.Baseurl_cui + Urls.issuetask_zhaoshanghu, getActivity(), 1, map);

你可能感兴趣的:(android)