基于retrofit2的请求管理类

写的一个基于retrofit2的请求管理类,用于管理请求的增删改查以及请求复用,可放在BaseActivity或者BaseFragment中;

package com.nbniu.app.common.tool;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import retrofit2.Call;

/**
 * 文件描述 :
 * 创建人员 : 幽默GUY
 * 创建日期 : 2018/8/2 0002
 * 创建时间 : 8:29
 * 最后修改 :
 */
public class RequestManager {

    private Map<String, Call> allRequest;

    public Map<String, Call> getAllRequest() {
        return allRequest;
    }

    /**
     * 得到随机标签,10位随机字符
     * @return
     */
    public static String getRandomTag() {
        return RandomStringTool.getRandomString(10);
    }

    /**
     * 添加请求
     * @param request
     * @param tag
     */
    public void addRequest(Call request, String tag) {
        if (allRequest == null)
            allRequest = new HashMap<>();

        Call oldRequest = getRequest(tag);
        if (oldRequest != null && !oldRequest.isCanceled()) {
            oldRequest.cancel();
        }
        allRequest.put(tag, request);
    }

    /**
     * 删除请求
     * @param tag
     */
    public void deleteRequest(String tag) {
        if (allRequest.containsKey(tag))
            allRequest.remove(tag);
    }

    /**
     * 得到请求
     * @param tag
     * @return
     */
    public Call getRequest(String tag) {
        return allRequest == null ? null : allRequest.get(tag);
    }

    /**
     * 如果请求执行过,则返回克隆对象
     * @param tag
     * @return
     */
    public Call getCloneRequest(String tag) {
        Call call = getRequest(tag);
        if (call == null)
            return null;

        return call.isExecuted() ? call.clone() : call;
    }

    /**
     * 是否包含请求
     * @param tag
     * @return
     */
    public boolean hasRequest(String tag) {
        return allRequest.containsKey(tag);
    }

    /**
     * 取消指定请求
     * @param tags
     */
    public void cancelRequest(String... tags) {
        for (String tag : tags) {
            Call call = getRequest(tag);
            if (call != null && !call.isCanceled())
                call.cancel();
        }
    }

    /**
     * 取消所有请求
     */
    public void cancelAll() {
        if (allRequest != null) {
            Iterator<String> iterator = allRequest.keySet().iterator();
            while (iterator.hasNext()) {
                Call call = allRequest.get(iterator.next());
                if (!call.isCanceled())
                    call.cancel();
            }
        }
    }
}


使用示例:

private RequestManager requestManager;

private final String TAG_DATA = RequestManager.getRandomTag();

private void loadData() {
	//在请求参数不发生改变的情况下:
	//每次请求参数会发生改变的情况下就生成新的请求,调用addRequest传入tag就可以了,会取消并替换掉原来的请求
	Call<Order> call = getRequestManager().getCloneRequest(TAG_DATA);
	if (call == null) {
		call = RequestTool.getTokenRequest().create(OrderRtService.class).getById(orderId);
		requestManager.addRequest(TAG_DATA);
	}
	//call.enqueue()
	//下面是我封装后的请求类执行
	new Request<Order>(call, this) {
        @Override
        public boolean dispose(Call<Order> call, Response<Order> response) {
        	//处理请求,返回处理结果
            return false;
        }

        @Override
        public void onError(Call<Order> call, Response<Order> response) {
        	//失败后动作
        }
     }.execute();
}

private RequestManager getRequestManager() {
	if (requestManager == null)
		requestManager = new RequestManager();
	return requestManager;
}

/**
  * 销毁前取消所有请求
  */
@Override
public void onDestroy() {
    if (requestManager != null)
        requestManager.cancelAll();
    super.onDestroy();
}

相关链接:
示例中用到的Reqeust请求封装类:
好用的retrofit2请求封装类
RandomStringTool工具类:
java生成指定长度随机字符串工具类

发布了10 篇原创文章 · 获赞 0 · 访问量 4388

猜你喜欢

转载自blog.csdn.net/qq_34786422/article/details/86479372
今日推荐