Skip to content

Commit 355853e

Browse files
committed
从 JCenter 迁移到 JitPack
优化框架逻辑嵌套及使用文档 适配 Android 分区存储特性 修正 Delete 请求的传参方式 新增支持 Options 请求方式 新增支持 Trace 请求方式 新增支持读取父类字段作为接口参数 新增支持 MultipartBody.Part 类型做为流参数 修复 EasyUtils.beanToHashMap 出现空指针的问题
1 parent b2967fa commit 355853e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1158
-720
lines changed

EasyHttp.apk

-108 KB
Binary file not shown.

HelpDoc.md

+52-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272

7373
* [框架只能传入 LifecycleOwner 该怎么办](#框架只能传入-lifecycleowner-该怎么办)
7474

75+
* [我想取消请求时显示的加载对话框该怎么办](#我想取消请求时显示的加载对话框该怎么办)
76+
7577
* [搭配 RxJava](#搭配-rxjava)
7678

7779
* [准备工作](#准备工作)
@@ -184,8 +186,8 @@ EasyConfig.getInstance()
184186
-dontwarn okhttp3.**
185187
-dontwarn okio.**
186188
187-
# 不混淆这个包下的字段名
188-
-keepclassmembernames class com.xxx.xxx.xxx.xxx.** {
189+
# 不混淆这个包下的类
190+
-keep class com.xxx.xxx.xxx.xxx.** {
189191
<fields>;
190192
}
191193
```
@@ -358,7 +360,7 @@ EasyHttp.post(this)
358360
});
359361
```
360362

361-
* 需要注意的是:如果上传的文件过大,可能会导致请求超时,可以重新设置本次请求超时的时间,具体设置超时方式文档有介绍,可以在本页面直接搜索。
363+
* **需要注意的是:如果上传的文件过多或者过大,可能会导致请求超时,可以重新设置本次请求超时时间,超时时间建议根据文件大小而定,具体设置超时方式文档有介绍,可以在本页面直接搜索。**
362364

363365
#### 下载文件
364366

@@ -912,6 +914,8 @@ public interface IRequestHandler {
912914
}
913915
```
914916

917+
* 如果你想对某个接口进行加解密,可以根据方法中的 api 参数对象来判断 ,如果你想对部分接口进行加解密,可以让外层的 IRequestApi 类实现统一的接口来标识这些接口,然后在 requestStart 和 requestFail 方法中判断 api 参数对象是否实现了这个接口来决定要不要进行加解密。
918+
915919
#### 如何对接口路径进行动态化拼接
916920

917921
```java
@@ -1050,6 +1054,51 @@ EasyHttp.post(new ApplicationLifecycle())
10501054
EasyHttp.cancel("abc");
10511055
```
10521056

1057+
#### 我想取消请求时显示的加载对话框该怎么办
1058+
1059+
* 首先这个加载对话框不是框架自带的,是可以修改或者取消的,主要有两种方式可供选择
1060+
1061+
* 第一种方式:重写 HttpCallback 类方法
1062+
1063+
```java
1064+
EasyHttp.post(this)
1065+
.api(new XxxApi())
1066+
.request(new HttpCallback<Xxx>(this) {
1067+
1068+
@Override
1069+
public void onStart(Call call) {
1070+
// 重写方法并注释父类调用
1071+
//super.onStart(call);
1072+
}
1073+
1074+
@Override
1075+
public void onEnd(Call call) {
1076+
// 重写方法并注释父类调用
1077+
//super.onEnd(call);
1078+
}
1079+
});
1080+
```
1081+
1082+
* 第二种方式:直接实现 OnHttpListener 接口
1083+
1084+
```java
1085+
1086+
EasyHttp.post(this)
1087+
.api(new XxxApi())
1088+
.request(new OnHttpListener<Xxx>() {
1089+
1090+
@Override
1091+
public void onSucceed(Xxx result) {
1092+
1093+
}
1094+
1095+
@Override
1096+
public void onFail(Exception e) {
1097+
1098+
}
1099+
});
1100+
```
1101+
10531102
# 搭配 RxJava
10541103

10551104
#### 准备工作

README.md

+42-14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,23 @@
2222

2323
* [OkHttp 精讲:CallServerInterceptor](https://www.jianshu.com/p/aa77af6251ff)
2424

25-
#### Gradle 集成
25+
#### 集成步骤
26+
27+
* 在项目根目录下的 `build.gradle` 文件中加入
28+
29+
```groovy
30+
buildscript {
31+
......
32+
}
33+
allprojects {
34+
repositories {
35+
// JitPack 远程仓库:https://jitpack.io
36+
maven { url 'https://jitpack.io' }
37+
}
38+
}
39+
```
40+
41+
* 在项目 app 模块下的 `build.gradle` 文件中加入
2642

2743
```groovy
2844
android {
@@ -35,7 +51,7 @@ android {
3551
3652
dependencies {
3753
// 网络请求框架:https://github.com/getActivity/EasyHttp
38-
implementation 'com.hjq:http:9.2'
54+
implementation 'com.github.getActivity:EasyHttp:9.5'
3955
// OkHttp 框架:https://github.com/square/okhttp
4056
// noinspection GradleDependency
4157
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
@@ -46,10 +62,10 @@ dependencies {
4662

4763
### 不同网络请求框架之间的对比
4864

49-
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) + [RxJava](https://github.com/ReactiveX/RxJava) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
65+
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
5066
| :----: | :------: | :-----: | :-----: |
51-
| 对应版本 | 9.2 | 2.9.0 | 3.0.4 |
52-
| **aar 包大小** | [63 KB](https://bintray.com/getactivity/maven/http#files/com/hjq/http) | [123 KB](https://bintray.com/bintray/jcenter/com.squareup.retrofit2%3Aretrofit#files) | [131 KB](https://bintray.com/jeasonlzy/maven/okgo#files/com/lzy/net/okgo) |
67+
| 对应版本 | 9.5 | 2.9.0 | 3.0.4 |
68+
| **aar 包大小** | [70 KB](https://jitpack.io/#getActivity/EasyHttp) | [123 KB](https://bintray.com/bintray/jcenter/com.squareup.retrofit2%3Aretrofit#files) | [131 KB](https://bintray.com/jeasonlzy/maven/okgo#files/com/lzy/net/okgo) |
5369
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
5470
| 配置多域名 ||||
5571
| **动态 Host** ||||
@@ -62,7 +78,7 @@ dependencies {
6278
| 上传进度监听 ||||
6379
| Json 参数提交 ||||
6480
| **请求代码定位** ||||
65-
| **延迟发起请求** || ||
81+
| **延迟发起请求** || ||
6682
| 上传文件类型 | File / InputStream / RequestBody | RequestBody | File |
6783
| **请求生命周期** | 自动管控 | 需要封装 | 需要封装 |
6884
| 参数传值方式 | 字段名 + 字段值 | 参数名 + 参数值 | 定义 Key + Value |
@@ -74,7 +90,17 @@ dependencies {
7490

7591
* Retrofit 在我看来并不是那么好用,因为很多常用的功能实现起来比较麻烦,动态 Host 要写拦截器,日志打印要写拦截器,就连最常用的添加全局参数也要写拦截器,一个拦截器意味着要写很多代码,如果写得不够严谨还有可能出现 Bug,从而影响整个 OkHttp 请求流程,我经常在想这些功能能不能都用一句代码搞定,因为我觉得这些功能是设计框架的时候本应该考虑的,这便是我做这个框架的初心。
7692

77-
* 本框架采用了 OOP 思想,一个请求代表一个对象,通过类继承和实现的特性来对接口进行动态化配置,几乎涵盖接口开发中所有的功能,使用起来非常简单灵活。而 Retrofit 采用的是注解方式,缺点是灵活性极低,因为注解上面只能放常量,也就会限定你在注解上面的一切参数只能是事先定义好的,这对接口的动态化配置极不利的。
93+
* OkGo 其实也存在一些弊端,例如会把参数的 key 引用放到外层去,这样会引发一些问题:
94+
95+
1. Key 管理问题:这个 key 可能会在外层被使用很多次,这样参数的 key 管理就会变得不可控,后续接口改动可能会出现漏改的风险,尽管这种情况比较少见,但是也不容忽视,而 EasyHttp 没有这个问题,因为 EasyHttp 不会将参数 key 值放置到外层中去。
96+
97+
2. 接口参数注释的问题:站在代码的规范角度上讲,我们应该在代码中注明参数的含义及作用,如果一旦将 key 放到外层,那么每一处调用的地方都需要写一遍注释,而 EasyHttp 是将参数字段化,只需要写一次注释到字段上即可。
98+
99+
3. 接口信息完整信息展示:使用 OkGo 请求网络,只能在调用的地方看到传递的接口参数,而一些被其他地方引用的参数,我们无法很直观的看到,只能通过追踪代码或者查看文档来得知,而 EasyHttp 将一个接口的信息全部通过一个类来管理的,这个类其实就相当于一个接口文档。
100+
101+
4. 接口的动态化配置:除了接口的参数之外,一个接口还有可能单独配置OkHttpClient 对象、参数的提交方式、接口响应处理方式等,这些用 OkGo 是可以实现,但是每个地方都要写一次,而 EasyHttp 可以直接在 API 类中配置,真正做到一劳永逸。
102+
103+
* EasyHttp 采用了 OOP 思想,一个请求代表一个对象,通过类继承和实现的特性来对接口进行动态化配置,几乎涵盖接口开发中所有的功能,使用起来非常简单灵活。而 Retrofit 采用的是注解方式,缺点是灵活性极低,因为注解上面只能放常量,也就会限定你在注解上面的一切参数只能是事先定义好的,这对接口的动态化配置极不利的。
78104

79105
* 有很多人觉得写一个接口类很麻烦,这个点确实有点麻烦,但是这块的付出是有收获的,从前期开发的效率考虑:OkGo > EasyHttp > Retrofit,但是从后期维护的效率考虑:EasyHttp > Retrofit > OkGo,之所以比较这三个框架,是因为框架的设计思想不同,但是我始终认为 EasyHttp 才是最好的设计,所以我创造了它。
80106

@@ -100,12 +126,14 @@ public final class HttpLifecycleManager implements LifecycleEventObserver {
100126

101127
@Override
102128
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
103-
if (event == Lifecycle.Event.ON_DESTROY) {
104-
// 移除监听
105-
source.getLifecycle().removeObserver(this);
106-
// 取消请求
107-
EasyHttp.cancel(source);
129+
if (event != Lifecycle.Event.ON_DESTROY) {
130+
return;
108131
}
132+
133+
// 移除监听
134+
source.getLifecycle().removeObserver(this);
135+
// 取消请求
136+
EasyHttp.cancel(source);
109137
}
110138
}
111139
```
@@ -173,9 +201,9 @@ EasyHttp.post(this)
173201

174202
#### 特别感谢
175203

176-
[张鸿洋](https://github.com/hongyangAndroid)
204+
* [张鸿洋](https://github.com/hongyangAndroid)
177205

178-
[WanAndroid](https://www.wanandroid.com/)
206+
* [WanAndroid](https://www.wanandroid.com/)
179207

180208
## License
181209

app/build.gradle

+23-18
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ android {
77
abortOnError false
88
}
99

10-
// 支持 JDK 1.8
11-
compileOptions {
12-
targetCompatibility JavaVersion.VERSION_1_8
13-
sourceCompatibility JavaVersion.VERSION_1_8
14-
}
15-
1610
defaultConfig {
1711
applicationId 'com.hjq.easy.demo'
1812
minSdkVersion 16
1913
targetSdkVersion 30
20-
versionCode 92
21-
versionName '9.2'
14+
versionCode 95
15+
versionName '9.5'
16+
}
17+
18+
// 支持 JDK 1.8
19+
compileOptions {
20+
targetCompatibility JavaVersion.VERSION_1_8
21+
sourceCompatibility JavaVersion.VERSION_1_8
2222
}
2323

2424
buildTypes {
@@ -40,23 +40,28 @@ dependencies {
4040

4141
implementation project(':library')
4242

43+
// OkHttp 框架:https://github.com/square/okhttp
44+
// 升级注意事项:https://www.jianshu.com/p/d12d0f536f55
45+
// noinspection GradleDependency
46+
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
47+
4348
// 谷歌兼容库:https://developer.android.google.cn/jetpack/androidx/releases/appcompat?hl=zh-cn
4449
implementation 'androidx.appcompat:appcompat:1.2.0'
4550

46-
// 标题栏框架:https://github.com/getActivity/TitleBar
47-
implementation 'com.hjq:titlebar:8.5'
4851
// 吐司框架:https://github.com/getActivity/ToastUtils
49-
implementation 'com.hjq:toast:8.8'
52+
implementation 'com.github.getActivity:ToastUtils:9.1'
53+
5054
// 权限请求框架:https://github.com/getActivity/XXPermissions
51-
implementation 'com.hjq:xxpermissions:10.5'
55+
implementation 'com.github.getActivity:XXPermissions:10.8'
56+
57+
// 标题栏框架:https://github.com/getActivity/TitleBar
58+
implementation 'com.github.getActivity:TitleBar:8.6'
59+
5260
// Json 解析框架:https://github.com/google/gson
5361
implementation 'com.google.code.gson:gson:2.8.6'
5462
// Gson 解析容错:https://github.com/getActivity/GsonFactory
55-
implementation 'com.hjq.gson:factory:5.2'
56-
// OkHttp 框架:https://github.com/square/okhttp
57-
// 升级注意事项:https://www.jianshu.com/p/d12d0f536f55
58-
// noinspection GradleDependency
59-
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
63+
implementation 'com.github.getActivity:GsonFactory:5.2'
64+
6065
// 日志调试框架:https://github.com/getActivity/Logcat
61-
debugImplementation 'com.hjq:logcat:9.5'
66+
debugImplementation 'com.github.getActivity:Logcat:9.6'
6267
}

app/proguard-rules.pro

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
-dontwarn okhttp3.**
3333
-dontwarn okio.**
3434

35-
# 不混淆这个包下的字段名
36-
-keepclassmembernames class com.hjq.easy.demo.http.** {
35+
# 不混淆这个包下的类
36+
-keep class com.hjq.easy.demo.http.** {
3737
<fields>;
3838
}

app/src/main/java/com/hjq/easy/demo/MainActivity.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
import androidx.core.content.ContextCompat;
1717
import androidx.core.content.FileProvider;
1818

19+
import com.hjq.easy.demo.http.api.SearchAuthorApi;
20+
import com.hjq.easy.demo.http.api.SearchBlogsApi;
21+
import com.hjq.easy.demo.http.api.UpdateImageApi;
1922
import com.hjq.easy.demo.http.model.HttpData;
20-
import com.hjq.easy.demo.http.request.SearchAuthorApi;
21-
import com.hjq.easy.demo.http.request.SearchBlogsApi;
22-
import com.hjq.easy.demo.http.request.UpdateImageApi;
23-
import com.hjq.easy.demo.http.response.SearchBean;
2423
import com.hjq.http.EasyHttp;
2524
import com.hjq.http.listener.HttpCallback;
2625
import com.hjq.http.listener.OnDownloadListener;
@@ -107,24 +106,24 @@ public void onClick(View view) {
107106

108107
EasyHttp.get(this)
109108
.api(new SearchAuthorApi()
110-
.setAuthor("鸿洋"))
111-
.request(new HttpCallback<HttpData<SearchBean>>(this) {
109+
.setId(190000))
110+
.request(new HttpCallback<HttpData<List<SearchAuthorApi.Bean>>>(this) {
112111

113112
@Override
114-
public void onSucceed(HttpData<SearchBean> result) {
113+
public void onSucceed(HttpData<List<SearchAuthorApi.Bean>> result) {
115114
ToastUtils.show("Get 请求成功,请看日志");
116115
}
117116
});
118117

119118
} else if (viewId == R.id.btn_main_post) {
120119

121-
EasyHttp.post(MainActivity.this)
120+
EasyHttp.post(this)
122121
.api(new SearchBlogsApi()
123122
.setKeyword("搬砖不再有"))
124-
.request(new HttpCallback<HttpData<SearchBean>>(MainActivity.this) {
123+
.request(new HttpCallback<HttpData<SearchBlogsApi.Bean>>(this) {
125124

126125
@Override
127-
public void onSucceed(HttpData<SearchBean> result) {
126+
public void onSucceed(HttpData<SearchBlogsApi.Bean> result) {
128127
ToastUtils.show("Post 请求成功,请看日志");
129128
}
130129
});
@@ -135,10 +134,10 @@ public void onSucceed(HttpData<SearchBean> result) {
135134
new Thread(() -> {
136135
runOnUiThread(this::showDialog);
137136
try {
138-
HttpData<SearchBean> data = EasyHttp.post(MainActivity.this)
137+
HttpData<SearchBlogsApi.Bean> data = EasyHttp.post(MainActivity.this)
139138
.api(new SearchBlogsApi()
140139
.setKeyword("搬砖不再有"))
141-
.execute(new ResponseClass<HttpData<SearchBean>>() {});
140+
.execute(new ResponseClass<HttpData<SearchBlogsApi.Bean>>() {});
142141
ToastUtils.show("同步请求成功,请看日志");
143142
} catch (Exception e) {
144143
e.printStackTrace();
@@ -196,6 +195,7 @@ public void onEnd(Call call) {
196195
ToastUtils.show("当前正在上传或者下载,请等待完成之后再进行操作");
197196
return;
198197
}
198+
199199
EasyHttp.download(this)
200200
.method(HttpMethod.GET)
201201
.file(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "微信.apk"))
@@ -230,7 +230,8 @@ public void onEnd(File file) {
230230
mProgressBar.setVisibility(View.GONE);
231231
}
232232

233-
}).start();
233+
})
234+
.start();
234235
}
235236
}
236237

0 commit comments

Comments
 (0)