Skip to content

Commit 6420cfa

Browse files
committed
优化框架代码逻辑
优化框架方法和类命名 修复延迟请求无法被取消的问题 修复数组类型参数无法被正常解析的问题 修复在多线程情况下使用异步请求会出现索引越界的问题
1 parent a3b83c4 commit 6420cfa

File tree

20 files changed

+197
-114
lines changed

20 files changed

+197
-114
lines changed

.github/ISSUE_TEMPLATE/issue_template_bug.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ assignees: getActivity
1616

1717
* 复现步骤【必填】:XXX(注意:目前不受理没有复现步骤的 Bug 单)
1818

19-
* 是否必现【必填】:填是/否
19+
* 是否必现【必填】:是/否
20+
21+
* 项目 targetSdkVersion【必填】:XX
2022

2123
* 出现问题的手机信息【必填】:请填写出现问题的品牌和机型
2224

HelpDoc.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
* [如何设置请求超时时间](#如何设置请求超时时间)
6666

6767
* [如何设置不打印日志](#如何设置不打印日志)
68-
68+
6969
* [如何修改日志打印策略](#如何修改日志打印策略)
7070

7171
* [如何取消已发起的请求](#如何取消已发起的请求)
@@ -443,7 +443,7 @@ EasyHttp.post(this)
443443

444444
@Override
445445
public void onSucceed(Xxx<Xxx> data) {
446-
446+
447447
}
448448
});
449449
```
@@ -481,7 +481,7 @@ public final class RequestHandler implements IRequestHandler {
481481
mApplication = application;
482482
mMmkv = MMKV.mmkvWithID("http_cache_id");
483483
}
484-
484+
485485
..................
486486

487487
@Nullable
@@ -717,7 +717,7 @@ public final class XxxApi implements IRequestApi {
717717
public String getApi() {
718718
return "xxx/xxxx";
719719
}
720-
720+
721721
@HttpIgnore
722722
private String token;
723723
}
@@ -980,7 +980,7 @@ public final class XxxApi implements IRequestApi {
980980
public String getApi() {
981981
return "xxx/xxxx";
982982
}
983-
983+
984984
@HttpIgnore
985985
private String address;
986986
}
@@ -996,7 +996,7 @@ public final class XxxApi implements IRequestApi {
996996
public String getApi() {
997997
return "xxx/xxxx";
998998
}
999-
999+
10001000
@HttpHeader
10011001
private String time;
10021002
}
@@ -1012,7 +1012,7 @@ public final class XxxApi implements IRequestApi {
10121012
public String getApi() {
10131013
return "xxx/xxxx";
10141014
}
1015-
1015+
10161016
@HttpRename("k")
10171017
private String keyword;
10181018
}
@@ -1024,13 +1024,13 @@ public final class XxxApi implements IRequestApi {
10241024

10251025
```java
10261026
public final class XxxApi implements IRequestApi {
1027-
1027+
10281028
@NonNull
10291029
@Override
10301030
public String getApi() {
10311031
return "xxx/xxxx";
10321032
}
1033-
1033+
10341034
private File file;
10351035
}
10361036
```
@@ -1039,13 +1039,13 @@ public final class XxxApi implements IRequestApi {
10391039

10401040
```java
10411041
public final class XxxApi implements IRequestApi {
1042-
1042+
10431043
@NonNull
10441044
@Override
10451045
public String getApi() {
10461046
return "xxx/xxxx";
10471047
}
1048-
1048+
10491049
private InputStream inputStream;
10501050
}
10511051
```
@@ -1060,7 +1060,7 @@ public final class XxxApi implements IRequestApi {
10601060
public String getApi() {
10611061
return "xxx/xxxx";
10621062
}
1063-
1063+
10641064
private RequestBody requestBody;
10651065
}
10661066
```
@@ -1075,7 +1075,7 @@ public final class XxxApi implements IRequestApi {
10751075
public String getApi() {
10761076
return "xxx/xxxx";
10771077
}
1078-
1078+
10791079
private List<File> files;
10801080
}
10811081
```
@@ -1147,7 +1147,7 @@ EasyConfig.with(okHttpClient)
11471147
* 需要修改日志打印策略的场景
11481148

11491149
* 需要将请求的日志写入到本地
1150-
1150+
11511151
* 需要修改打印的请求日志格式
11521152

11531153
#### 如何取消已发起的请求
@@ -1172,7 +1172,7 @@ EasyHttp.post(MainActivity.this)
11721172

11731173
@Override
11741174
public void onSucceed(HttpData<XxxBean> result) {
1175-
1175+
11761176
}
11771177
});
11781178
```
@@ -1207,7 +1207,7 @@ EasyHttp.post(this)
12071207

12081208
@Override
12091209
public void onSucceed(Xxx result) {
1210-
1210+
12111211
}
12121212
});
12131213
```
@@ -1394,7 +1394,7 @@ EasyHttp.post(this)
13941394

13951395
@Override
13961396
public void onSucceed(Xxx result) {
1397-
1397+
13981398
}
13991399

14001400
@Override
@@ -1493,7 +1493,7 @@ EasyHttp.post(this)
14931493

14941494
@Override
14951495
public void onSucceed(HttpData<Xxx> result) {
1496-
1496+
14971497
}
14981498
});
14991499
```
@@ -1590,7 +1590,7 @@ EasyHttp.get(this)
15901590

15911591
@Override
15921592
public void onSucceed(Xxx result) {
1593-
1593+
15941594
}
15951595
});
15961596
```
@@ -1620,7 +1620,7 @@ Observable.create(new ObservableOnSubscribe<HttpData<SearchBean>>() {
16201620

16211621
@Override
16221622
public void subscribe(ObservableEmitter<HttpData<SearchBean>> emitter) throws Exception {
1623-
1623+
16241624
HttpData<SearchBean> data1;
16251625
try {
16261626
data1 = EasyHttp.post(MainActivity.this)
@@ -1657,7 +1657,7 @@ Observable.create(new ObservableOnSubscribe<HttpData<SearchBean>>() {
16571657
}
16581658

16591659
}, new Consumer<Throwable>() {
1660-
1660+
16611661
@Override
16621662
public void accept(Throwable throwable) throws Exception {
16631663
toast(throwable.getMessage());
@@ -1738,9 +1738,9 @@ Observable.create(new ObservableOnSubscribe<HttpData<SearchBean>>() {
17381738
public void accept(String s) throws Exception {
17391739
Log.i("EasyHttp", ""当前页码位置" + s);
17401740
}
1741-
1741+
17421742
}, new Consumer<Throwable>() {
1743-
1743+
17441744
@Override
17451745
public void accept(Throwable throwable) throws Exception {
17461746
toast(throwable.getMessage());

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
* 博客地址:[网络请求,如斯优雅](https://www.jianshu.com/p/93cd59dec002)
66

7-
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/11.6/EasyHttp.apk)
7+
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处下载Demo](https://github.com/getActivity/EasyHttp/releases/download/11.8/EasyHttp.apk)
88

99
![](picture/demo_code.png)
1010

@@ -61,24 +61,24 @@ android {
6161
6262
dependencies {
6363
// 网络请求框架:https://github.com/getActivity/EasyHttp
64-
implementation 'com.github.getActivity:EasyHttp:11.6'
64+
implementation 'com.github.getActivity:EasyHttp:11.8'
6565
// OkHttp 框架:https://github.com/square/okhttp
6666
// noinspection GradleDependency
6767
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
6868
}
6969
```
7070

7171
* 需要注意的是:由于使用了 Lifecycle 特性,目前框架只支持 AndroidX 的项目集成
72-
72+
7373
## [框架的具体用法请点击这里查看](HelpDoc.md)
7474

7575
### 不同网络请求框架之间的对比
7676

7777
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
7878
| :----: | :------: | :-----: | :-----: |
79-
| 对应版本 | 11.6 | 2.9.0 | 3.0.4 |
79+
| 对应版本 | 11.8 | 2.9.0 | 3.0.4 |
8080
| issues 数 | [![](https://img.shields.io/github/issues/getActivity/EasyHttp.svg)](https://github.com/getActivity/EasyHttp/issues) | [![](https://img.shields.io/github/issues/square/retrofit.svg)](https://github.com/square/retrofit/issues) | [![](https://img.shields.io/github/issues/jeasonlzy/okhttp-OkGo.svg)](https://github.com/jeasonlzy/okhttp-OkGo/issues) |
81-
| **aar 包大小** | 89 KB | 123 KB | 131 KB |
81+
| **aar 包大小** | 90 KB | 123 KB | 131 KB |
8282
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
8383
| 配置多域名 ||||
8484
| **动态 Host** ||||
@@ -109,9 +109,9 @@ dependencies {
109109
* OkGo 其实也存在一些弊端,例如会把参数的 key 引用放到外层去,这样会引发一些问题:
110110

111111
1. Key 管理问题:这个 key 可能会在外层被使用很多次,这样参数的 key 管理就会变得不可控,后续接口改动可能会出现漏改的风险,尽管这种情况比较少见,但是也不容忽视,而 EasyHttp 没有这个问题,因为 EasyHttp 不会将参数 key 值放置到外层中去。
112-
112+
113113
2. 接口参数注释的问题:站在代码的规范角度上讲,我们应该在代码中注明参数的含义及作用,如果一旦将 key 放到外层,那么每一处调用的地方都需要写一遍注释,而 EasyHttp 是将参数字段化,只需要写一次注释到字段上即可。
114-
114+
115115
3. 接口信息完整信息展示:使用 OkGo 请求网络,只能在调用的地方看到传递的接口参数,而一些被其他地方引用的参数,我们无法很直观的看到,只能通过追踪代码或者查看文档来得知,而 EasyHttp 将一个接口的信息全部通过一个类来管理的,这个类其实就相当于一个接口文档。
116116

117117
4. 接口的动态化配置:除了接口的参数之外,一个接口还有可能单独配置 OkHttpClient 对象、参数的提交方式、接口响应处理方式等,这些用 OkGo 是可以实现,但是每个地方都要写一次,而 EasyHttp 可以直接在 API 类中配置,真正做到一劳永逸。
@@ -127,7 +127,7 @@ public final class XxxApi implements IRequestApi {
127127
public String getApi() {
128128
return "xxx/xxx";
129129
}
130-
130+
131131
private int xxx;
132132

133133
public XxxApi setXxx(int xxx) {
@@ -166,7 +166,7 @@ public final class HttpLifecycleManager implements LifecycleEventObserver {
166166
/**
167167
* 绑定组件的生命周期
168168
*/
169-
public static void bind(LifecycleOwner lifecycleOwner) {
169+
public static void register(LifecycleOwner lifecycleOwner) {
170170
lifecycleOwner.getLifecycle().addObserver(new HttpLifecycleManager());
171171
}
172172

@@ -208,7 +208,7 @@ EasyHttp.post(this)
208208

209209
@Override
210210
public void onSucceed(HttpData<XxxBean> result) {
211-
211+
212212
}
213213
});
214214
```
@@ -249,6 +249,8 @@ EasyHttp.post(this)
249249

250250
* 表情包大集合:[EmojiPackage](https://github.com/getActivity/EmojiPackage) ![](https://img.shields.io/github/stars/getActivity/EmojiPackage.svg) ![](https://img.shields.io/github/forks/getActivity/EmojiPackage.svg)
251251

252+
* AI 资源大汇总:[AiIndex](https://github.com/getActivity/AiIndex) ![](https://img.shields.io/github/stars/getActivity/AiIndex.svg) ![](https://img.shields.io/github/forks/getActivity/AiIndex.svg)
253+
252254
* 省市区 Json 数据:[ProvinceJson](https://github.com/getActivity/ProvinceJson) ![](https://img.shields.io/github/stars/getActivity/ProvinceJson.svg) ![](https://img.shields.io/github/forks/getActivity/ProvinceJson.svg)
253255

254256
* Markdown 语法文档:[MarkdownDoc](https://github.com/getActivity/MarkdownDoc) ![](https://img.shields.io/github/stars/getActivity/MarkdownDoc.svg) ![](https://img.shields.io/github/forks/getActivity/MarkdownDoc.svg)

app/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ android {
1414
applicationId 'com.hjq.easy.demo'
1515
minSdkVersion 21
1616
targetSdkVersion 31
17-
versionCode 1160
18-
versionName '11.6'
17+
versionCode 1180
18+
versionName '11.8'
1919
}
2020

2121
// 支持 JDK 1.8
@@ -76,10 +76,10 @@ dependencies {
7676
implementation 'com.github.getActivity:Toaster:12.0'
7777

7878
// 权限请求框架:https://github.com/getActivity/XXPermissions
79-
implementation 'com.github.getActivity:XXPermissions:16.6'
79+
implementation 'com.github.getActivity:XXPermissions:16.8'
8080

8181
// 标题栏框架:https://github.com/getActivity/TitleBar
82-
implementation 'com.github.getActivity:TitleBar:10.0'
82+
implementation 'com.github.getActivity:TitleBar:10.2'
8383

8484
// Json 解析框架:https://github.com/google/gson
8585
implementation 'com.google.code.gson:gson:2.10.1'

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import androidx.appcompat.app.AppCompatActivity;
66

7-
import com.hjq.easy.demo.http.model.HttpData;
87
import com.hjq.http.listener.OnHttpListener;
98
import com.hjq.toast.Toaster;
109

@@ -66,11 +65,7 @@ public void onStart(Call call) {
6665
}
6766

6867
@Override
69-
public void onSucceed(Object result) {
70-
if (result instanceof HttpData) {
71-
Toaster.show(((HttpData<?>) result).getMessage());
72-
}
73-
}
68+
public void onSucceed(Object result) {}
7469

7570
@Override
7671
public void onFail(Exception e) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.hjq.easy.demo.http.model.HttpData;
2626
import com.hjq.http.EasyHttp;
2727
import com.hjq.http.EasyUtils;
28-
import com.hjq.http.exception.FileMD5Exception;
28+
import com.hjq.http.exception.FileMd5Exception;
2929
import com.hjq.http.listener.HttpCallback;
3030
import com.hjq.http.listener.OnDownloadListener;
3131
import com.hjq.http.listener.OnUpdateListener;
@@ -288,7 +288,7 @@ public void onComplete(File file) {
288288
@Override
289289
public void onError(File file, Exception e) {
290290
Toaster.show(e.getMessage());
291-
if (e instanceof FileMD5Exception) {
291+
if (e instanceof FileMd5Exception) {
292292
// 如果是文件 md5 校验失败,则删除文件
293293
file.delete();
294294
}

app/src/main/java/com/hjq/easy/demo/http/model/RequestHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.hjq.http.config.IRequestHandler;
2020
import com.hjq.http.exception.CancelException;
2121
import com.hjq.http.exception.DataException;
22-
import com.hjq.http.exception.FileMD5Exception;
22+
import com.hjq.http.exception.FileMd5Exception;
2323
import com.hjq.http.exception.HttpException;
2424
import com.hjq.http.exception.NetworkException;
2525
import com.hjq.http.exception.NullBodyException;
@@ -188,8 +188,8 @@ public Exception downloadFail(@NonNull HttpRequest<?> httpRequest, @NonNull Exce
188188
NullBodyException nullBodyException = ((NullBodyException) e);
189189
nullBodyException.setMessage(mApplication.getString(R.string.http_response_null_body));
190190
return nullBodyException;
191-
} else if (e instanceof FileMD5Exception) {
192-
FileMD5Exception fileMd5Exception = ((FileMD5Exception) e);
191+
} else if (e instanceof FileMd5Exception) {
192+
FileMd5Exception fileMd5Exception = ((FileMd5Exception) e);
193193
fileMd5Exception.setMessage(mApplication.getString(R.string.http_response_md5_error));
194194
return fileMd5Exception;
195195
}

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android {
55

66
defaultConfig {
77
minSdkVersion 14
8-
versionCode 1160
9-
versionName "11.6"
8+
versionCode 1180
9+
versionName "11.8"
1010
}
1111

1212
// 使用 JDK 1.8

0 commit comments

Comments
 (0)