代码二次封装-xUtils(android)

通常我们会引用许多lib
并且会出现lib 与我们的功能只差一点点 这样的情况我们最好不要去修改源码 而是进行二次封装

举例我使用 xUtils的二次封装 此处说明我是搞ios的 这个是android 今天mac 机子没网 蛋疼

使用到pulltorefresh 和 xUtils

  1. 网络封装
    由于此处是没有文件处理所以我把参数转换里面file给去掉了 如果需要自行加上即可

public class ApiHttpUtil {
    final public static String base_url_api = Constants.base_url_api;
    private String api_url;
    HttpApiCallBack callBack;
    Map<String, String> paramsMap;
    CacheEntity cacheEntity;

    Long timeOut = 0L;

    public ApiHttpUtil() {
    }

    public ApiHttpUtil(HttpApiCallBack callBack) {
        this.callBack = callBack;
        this.cacheEntity = new CacheEntity();
    }

    public ApiHttpUtil(String url, HttpApiCallBack callBack) {
        this.api_url = url;
        this.callBack = callBack;
        this.cacheEntity = new CacheEntity();
    }

    public String getApiUrl() {
        if (StringUtils.isEmpty(api_url)) {
            return base_url_api + "?";
        }
        if (api_url.startsWith("http")) {
            return api_url;
        }
        return base_url_api + "/" + api_url;
    }

    public String strValue(Object obj) {
        if (obj instanceof String) {
            return (String) obj;
        } else if (obj instanceof Integer) {
            return Integer.toString((Integer) obj);
        } else if (obj instanceof Double) {
            return Double.toString((Double) obj);
        } else {
            // TODO 其他 object 类转换 http string
        }
        return String.valueOf(obj);
    }

    public void getObjs(Object... objs) {
        Map<String, Object> map = getMap(objs);
        get(map);
    }

    public void postObjs(Object... objs) {
        Map<String, Object> map = getMap(objs);
        post(map);
    }

    /*
     * key --- String Integer Double value -- +File
     */
    public Map<String, Object> getMap(Object[] objs) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (objs.length % 2 == 0) {
            for (int i = 0; i < objs.length; i += 2) {
                try {
                    String key = (String) strValue(objs[i]);
                    Object value = null;
                    if (objs.length > i + 1) {
                        Object objs2 = objs[i + 1];
                        value = strValue(objs2);
                    }
                    if (value instanceof String) {
                        if (!StringUtils.isEmpty((String) value)
                                && !StringUtils.isEmpty(key)) {
                            map.put(key, value);
                        }
                    } else if (value instanceof File) {
                        map.put(key, value);
                    }
                } catch (Exception e) {
                }
            }
        }
        return map;
    }

    public void get() {
        get(null);
    }

    public void post() {
        post(null);
    }

    public ApiHttpUtil setTimeOut(int second) {
        this.timeOut = second * 1000L;
        return this;
    }

    public void post(Map<String, Object> map) {
        RequestParams params = new RequestParams();
        if (map != null) {
            for (Entry<String, Object> entry : map.entrySet()) {
                if (entry.getValue() instanceof String) {
                    params.addBodyParameter(entry.getKey(),
                            (String) entry.getValue());
                } else {
                    params.addBodyParameter(entry.getKey(),
                            strValue(entry.getValue()));
                }
            }
        }

        if (timeOut <= 0 && !useCache(map)) {
            send(HttpRequest.HttpMethod.POST, params);
        }
    }

    public void get(Map<String, Object> map) {

        RequestParams params = new RequestParams();
        if (map != null) {
            for (Entry<String, Object> entry : map.entrySet()) {
                if (entry.getValue() instanceof String) {
                    params.addQueryStringParameter(entry.getKey(),
                            (String) entry.getValue());
                } else {
                    params.addQueryStringParameter(entry.getKey(),
                            strValue(entry.getValue()));
                }
            }
        }
        if (timeOut <= 0 && !useCache(map)) {
            send(HttpRequest.HttpMethod.GET, params);
        }
    }

    public boolean useCache(Map<String, Object> map) {
        this.cacheEntity.setKey(getApiUrl() + map.toString());
        if (AppContext.mInstance != null) {
            if (!StringUtils.isEmpty(this.cacheEntity.getValue())) {
                if (!AppUtils.isNetworkAvailable(AppContext.mInstance)) {
                    AppUtils.openNetworkSettings(AppContext.mInstance);
                    onSuccess(this.cacheEntity.getCacheValue());
                    return true;
                }
            }
            String result = this.cacheEntity.getValue(timeOut);
            if (!StringUtils.isEmpty(result)) {
                onSuccess(result);
                return true;
            }
        }
        return false;
    }

    public void send(HttpRequest.HttpMethod method, RequestParams params) {
        HttpUtils http = new HttpUtils();
        http.send(method, getApiUrl(), params,
                new RequestCallBack<String>() {
                    @Override
                    public void onStart() {
                        callBack.onStart();
                    }

                    @Override
                    public void onLoading(long total, long current,
                            boolean isUploading) {
                        callBack.onLoading(total, current, isUploading);
                    }

                    @Override
                    public void onCancelled() {
                        callBack.onCancelled();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        ApiHttpUtil.this.onSuccess(responseInfo.result);
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        callBack.onFailure(error, msg);
                    }
                });
    }

    public boolean 
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值