LikelyHttp – 可能是最简单的retorfit二次封装库
retorfit + okhttp可能现阶段最常见的android网络请求库了,网上针对retorfit和okhttp二次封装的库层出不穷, 其中有很多很优秀功能也很强大的库,但功能强大和内容丰富很有可能带来了更多的耦合从而导致这些第三方的库在使用的时候不尽如人意,很多的时候我们的使用第三方的库的目的是使得我们的使用更加的简洁和简单. 二是网络库是一个使用频率十分高的库,为此为自己封装一个自己使用更加顺手的库,为此才有了个做了减法的LikeluHttp.
如何使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| OkHttpClient mOkHttpClient = new OkHttpClient.Builder() .addInterceptor(LogInterceptor()) .build();
Retrofit mRetrofit = new Retrofit.Builder() .baseUrl(BasUrl) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .client(mOkHttpClient) .build();
BaseNetApi baseNetApi = mRetrofit.create(BaseNetApi.class);
LikelyHttp.getInstance()
.setUniteDeal(new BaseObserverInterface() { @Override public void onRequestStart() { Toast.makeText(MainActivity.this, "onRequestStart", Toast.LENGTH_SHORT).show(); }
@Override public void onRequestEnd() { Toast.makeText(MainActivity.this, "onRequestEnd", Toast.LENGTH_SHORT).show(); }
@Override public void onCodeError(int errorCode) { Toast.makeText(MainActivity.this, "onCodeError: " + errorCode, Toast.LENGTH_SHORT).show(); } })
.setUniteDeal(200) ;
LikelyHttp.getInstance().start(baseNetApi.getHome(), new BaseObserver<String>(){ @Override protected void onSuccees(BaseEntity<String> t) throws Exception { Toast.makeText(MainActivity.this, "onSuccees", Toast.LENGTH_SHORT).show(); t.getData() }
@Override protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception { Toast.makeText(MainActivity.this, "onFailure", Toast.LENGTH_SHORT).show(); } });
LikelyHttp.getInstance().start(baseNetApi.getHome(), new BaseObserver<String>(false){ ... });
LikelyHttp.getInstance().start(baseNetApi.getHome(), new BaseObserver<String>(){ ... }, true);
|
其中BaseNetApi为平时使用的接口类,用于定义网络请求
1 2 3 4
| public interface BaseNetApi { @GET("clwater") Observable<BaseEntity<String>> getHome(); }
|
使用详情
- 简单get请求
模拟最简单的get success请求
- 简单get请求(失败)
模拟最简单的get fail请求
- 服务器返回状态码错误的情况(统一处理)
模拟服务器返回状态码错误的情况
- 简单的post请求
模拟简单的post请求
- 异步线程回调模拟
模拟异步线程回调模拟
如何封装
- BaseEntity 请求默认返回结构
BaseEntity暂时不支持自定义结构,后续会增加此部分的自定义情况
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| 请求默认返回格式
public class BaseEntity<T> { public static int SUCCESS_CODE = 200; private int status; private String msg; private T data;
public boolean isSuccess(){ return getCode() == SUCCESS_CODE; } public int getCode() { return status; }
public void setCode(int code) { this.status = code; }
public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; }
public T getData() { return data; }
public void setData(T data) { this.data = data; }
}
2. BaseObserver请求回调处理,包含每个请求的成功与失败以及统一的情况处理
public abstract class BaseObserver<T> implements Observer<BaseEntity<T>> {
private boolean userUniteDeal = true;
private static BaseObserverInterface baseObserverInterface;
public static void setBaseObserverInterface(BaseObserverInterface baseObserverInterface) { BaseObserver.baseObserverInterface = baseObserverInterface; }
public BaseObserver() {
}
public BaseObserver(boolean useLoading) { this.userUniteDeal = useLoading; }
@Override public void onSubscribe(Disposable d) { onRequestStart(); }
@Override public void onNext(BaseEntity<T> tBaseEntity) { onRequestEnd(); if (tBaseEntity.isSuccess()) { try { onSuccees(tBaseEntity); } catch (Exception e) { e.printStackTrace(); } } else { try { onCodeError(tBaseEntity); } catch (Exception e) { e.printStackTrace(); } } }
@Override public void onError(Throwable e) { onRequestEnd(); try { if (e instanceof ConnectException || e instanceof TimeoutException || e instanceof NetworkErrorException || e instanceof UnknownHostException) { onFailure(e, true); } else { onFailure(e, false); } } catch (Exception e1) { e1.printStackTrace(); } }
@Override public void onComplete() { }
protected void onCodeError(BaseEntity<T> t) throws Exception { baseObserverInterface.onCodeError(t.getCode()); }
protected abstract void onSuccees(BaseEntity<T> t) throws Exception;
protected abstract void onFailure(Throwable e, boolean isNetWorkError) throws Exception;
protected void onRequestStart() { if (userUniteDeal) { baseObserverInterface.onRequestStart(); } }
protected void onRequestEnd() { if (userUniteDeal) { baseObserverInterface.onRequestEnd(); } }
}
|
代码
相关代码可以访问我的GitHub