Volley的使用(三) 简单封装

1、建立自定义类
2、建立接口

将volley封装成一个工具,使用起来更加方便。
1、
建立自定义类:
VolleyRequest.java

public class VolleyRequest {
    public static StringRequest stringRequest;
    public static Context context;
    public static void RequestGet(
            Context mcontext,String url,String tag,VolleyInterface vif){
        //防止重复请求,所以先取消tag标识的请求队列
        MyApplication.getHttpQueues().cancelAll(tag);
        stringRequest=new StringRequest(Method.GET, url,
                vif.loadingListener(),
                vif.errorListener()
                );
        stringRequest.setTag(tag);
        MyApplication.getHttpQueues().add(stringRequest);

        //注意千万不要调用start来开启。这样写是不对的。
        //因为在源码里,当我们调用Volley.newRequestQueue()来实例化一个请求队列的时候
        //就已经使用queue.start(); 方法来开启了一个工作线程,所以我们如果多次调用
        //newRequestQueue来实例化请求队列就会多次调用start方法,这样做势必增加性能的消耗
        //所以我们一定要把volley的请求队列全局化(可以使用单例模式或在application初始化)。
        //并且我们不应当手动调用start。
//      MyApplication.getHttpQueues().start();
    }
    public static void RequestPost(Context context,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);
    }

}

2、建立接口:

VolleyInterface.java

package com.example.volley;

import com.android.volley.VolleyError;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import android.content.Context;

public abstract class VolleyInterface {
    private  Context mContext;
    public static Listener<String> mListener;
    public static ErrorListener mErrorListener;

    public VolleyInterface(Context context, Listener<String> listener, ErrorListener errorListener) {
        this.mContext = context;
        this.mListener = listener;
        this.mErrorListener = errorListener;
    }

    public abstract void onMySuccess(String result);
    public abstract void onMyError(VolleyError error);

    public Listener<String> loadingListener() {
        mListener = new Listener<String>() {
            @Override
            public void onResponse(String response) {
                    onMySuccess(response);
            }
        };
        return mListener;
    }

    public ErrorListener errorListener() {
        mErrorListener = new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                onMyError(error);
            }
        };
        return mErrorListener;

    }

}

3、使用方法:

    private void volley_Get() {
        String url = "";
        VolleyRequest.RequestGet(this, url, "124", new VolleyInterface(this, VolleyInterface.mListener, VolleyInterface.mErrorListener) {

            @Override
            public void onMySuccess(String result) {
                Toast.makeText(VolleySecond.this, result, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onMyError(VolleyError error) {

            }
        });
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值