RxEasyHttp 自定义ApiResult

public class RxHttp {

    private static RxHttp rxHttp;
    private Map<String, String> map;

    private RxHttp() {
    }

    public static synchronized RxHttp getInstance() {
        //每次调用都创建新的对象
        rxHttp = new RxHttp();
        return rxHttp;
    }

    public <T> void post(String url, final HttpCallBack<T> httpCallBack, final Class clazz) {
        PostRequest post = EasyHttp.post(url);
        //必须的参数
        post.params("partnerId", JrmfClient.getPartnerid())
                .params("deviceType", "android");

        //添加的参数
        if (map != null) {
            for (String key : map.keySet()) {
                Object o = map.get(key);
                if (o instanceof String) {
                    post.params(key, (String) o);
                }
            }
        }

        post.execute(new SimpleCallBack<String>() {
            @Override
            public void onError(ApiException e) {
                httpCallBack.onError(e);
            }

            @Override
            public void onSuccess(String response) {
                T t1 = (T) new Gson().fromJson(response, clazz);
                httpCallBack.onSuccess(t1);
            }
        });
    }

    //添加参数
    public RxHttp params(String key, String value) {
        if (map == null) {
            map = new HashMap();
        }
        if (StringUtil.isNotEmpty(value)) {
            map.put(key, value);
        }
        return rxHttp;
    }

    public interface HttpCallBack<T> {
        void onSuccess(T t);

        void onError(Exception e);
    }

}

你可能感兴趣的:(RxEasyHttp 自定义ApiResult)