如何运用回调和泛型来简洁代码

回调函数运用非常广泛,例如setOnClickListener就是button的一个回调。运用好回调函数可以使接口更直观。泛型则预留了可变部分,使代码更灵活简洁。
话不多说,上代码

public class VolleyTool {

    private Context mContext;
    private CallBackVolley cbv;
    private static RequestQueue mSingleQueue;
    public VolleyTool(Context context) {
        this.mContext = context;
        mSingleQueue = Volley.newRequestQueue(mContext.getApplicationContext(), new MultiPartStack());
    }
    /**
     *
     * @param url 请求地址
     * @param map 接口传入的参数
     * @param cls 实体类
     * @param callBackVolley 回调函数
     */
    public <T>void setCallBack(final String url, final Map<String, String> map,final Class<T> cls,final CallBackVolley callBackVolley){
        VolleyTool.this.cbv = callBackVolley;
        StringRequest stringRequest = new StringRequest(Request.Method.POST,
                url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // 使用JSONObject给response转换编码
                        Log.d("lxl", response);
                        if (response != null) {
                             T bean = GsonTools.changeGsonToBean(response, cls);
                             cbv.OnSuccessVolley(bean);
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                map.put("key", "z_for_example");
                return map;
            }
        };
        MyApplicationTools.mQueue.add(stringRequest);
    }
}
public interface CallBackVolley {

    public <T>void OnSuccessVolley(T bean);
    public <T>void OnFailedVolley(String message);
}

模拟登陆接口:

Map<String,String> map = new HashMap<String, String>();
         map.put("phonenum", "131xxxxxxxx");
         map.put("captcha", "xxxx");
        VolleyTool volleyTool = new VolleyTool(this);
        volleyTool.setCallBack(LOGIN_URL, map, LoginMessage.class, new CallBackVolley() {

            @Override
            public <T> void OnSuccessVolley(T bean) {
                LoginMessage loginMessage = (LoginMessage) bean;
                 if (loginMessage.result == 1) {
                     //登陆成功             
                }else{
                    //登陆失败
                }
            }

            @Override
            public <T> void OnFailedVolley(String message) {
                //请求失败
            }
        });

bean:

public class LoginMessage {

    public String message;
    public int result;
    public String userId;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值