凯哥巅峰之作/业界良心,一度被葬爱家族称作超越OkHttp的存在~
- 对OkHttpUtils进行了简单包装,访问网络更加简单,OkHttpUtils地址 >>https://github.com/hongyangAndroid/okhttputils
- 目前对应的OkHttp版本3.3.1,OkHttpUtils的版本2.6.2
- 一直在发现问题解决问题,还有很多不严谨的地方,仅供学习与交流
- 给个start鼓励一下吧,笔芯
1.在root project的build.gradle中添加中央仓库
maven{ url 'https://jitpack.io'}
2.添加依赖
compile 'com.github.luziming:VeryOkHttp:v1.0.2'
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(10000L, TimeUnit.MILLISECONDS)
.readTimeout(10000L, TimeUnit.MILLISECONDS)
.build();
OkHttpUtils.initClient(okHttpClient);
// 初始化对象,指定target
VeryOkHttp veryOkHttp = new VeryOkHttp(this);
// 添加参数
HttpParams params = new HttpParams();
// 请求头
params.addHeader(token);
params.put("key","value");
veryOkHttp.get(URL_GET,params,GET_CODE,this);
HttpParams params = new HttpParams();
params.addHeader(token);
//通过Gson将been对象转换为Json请求
UserParams userParams = new UserParams(new UserParams.UserBean(name,desc,""));
veryOkHttp.postJson(API.ADD_PRODUCTS_URL,params, API.ADD_PRODUCTS_CODE,userParams,this);
@Override
public void onSuccess(String response, int id) {
//可以通过id来区别response switch (id) { case GET_CODE: case POST_CODE: break; } }
@Override
public void onError(Call call, Exception e, int id) {
switch (id) {
case GET_CODE:
case POST_CODE:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
veryOkHttp.cancel(this);
}