Skip to content

Commit f90c894

Browse files
committed
兼容 MultipartBody.Builder 在 build 时参数为空会报错的情况
1 parent 355853e commit f90c894

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

EasyHttp.apk

55 Bytes
Binary file not shown.

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828

2929
```groovy
3030
buildscript {
31-
......
31+
repositories {
32+
maven { url 'https://jitpack.io' }
33+
}
3234
}
3335
allprojects {
3436
repositories {
35-
// JitPack 远程仓库:https://jitpack.io
3637
maven { url 'https://jitpack.io' }
3738
}
3839
}
@@ -51,7 +52,7 @@ android {
5152
5253
dependencies {
5354
// 网络请求框架:https://github.com/getActivity/EasyHttp
54-
implementation 'com.github.getActivity:EasyHttp:9.5'
55+
implementation 'com.github.getActivity:EasyHttp:9.6'
5556
// OkHttp 框架:https://github.com/square/okhttp
5657
// noinspection GradleDependency
5758
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
@@ -64,7 +65,7 @@ dependencies {
6465

6566
| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
6667
| :----: | :------: | :-----: | :-----: |
67-
| 对应版本 | 9.5 | 2.9.0 | 3.0.4 |
68+
| 对应版本 | 9.6 | 2.9.0 | 3.0.4 |
6869
| **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) |
6970
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
7071
| 配置多域名 ||||

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ android {
1111
applicationId 'com.hjq.easy.demo'
1212
minSdkVersion 16
1313
targetSdkVersion 30
14-
versionCode 95
15-
versionName '9.5'
14+
versionCode 96
15+
versionName '9.6'
1616
}
1717

1818
// 支持 JDK 1.8
@@ -49,7 +49,7 @@ dependencies {
4949
implementation 'androidx.appcompat:appcompat:1.2.0'
5050

5151
// 吐司框架:https://github.com/getActivity/ToastUtils
52-
implementation 'com.github.getActivity:ToastUtils:9.1'
52+
implementation 'com.github.getActivity:ToastUtils:9.2'
5353

5454
// 权限请求框架:https://github.com/getActivity/XXPermissions
5555
implementation 'com.github.getActivity:XXPermissions:10.8'

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 95
9-
versionName "9.5"
8+
versionCode 96
9+
versionName "9.6"
1010
}
1111

1212
// 使用 JDK 1.8

library/src/main/java/com/hjq/http/request/BodyRequest.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ public T request(OnHttpListener<?> listener) {
164164
* 组装 RequestBody 对象
165165
*/
166166
private RequestBody createBody(HttpParams params, BodyType type) {
167+
RequestBody body;
168+
167169
if (params.isMultipart() && !params.isEmpty()) {
168170
MultipartBody.Builder builder = new MultipartBody.Builder();
169171
builder.setType(MultipartBody.FORM);
@@ -219,29 +221,25 @@ private RequestBody createBody(HttpParams params, BodyType type) {
219221
builder.addFormDataPart(key, String.valueOf(object));
220222
}
221223

222-
if (mUpdateListener != null) {
223-
return new ProgressBody(builder.build(), getLifecycleOwner(), mUpdateListener);
224+
try {
225+
body = builder.build();
226+
} catch (IllegalStateException ignored) {
227+
// 如果参数为空则会抛出异常:Multipart body must have at least one part.
228+
body = new FormBody.Builder().build();
224229
}
225-
return builder.build();
226-
}
227-
228-
if (type == BodyType.JSON) {
229-
if (mUpdateListener != null) {
230-
return new ProgressBody(new JsonBody(params.getParams()), getLifecycleOwner(), mUpdateListener);
231-
}
232-
return new JsonBody(params.getParams());
233-
}
234230

235-
FormBody.Builder builder = new FormBody.Builder();
236-
if (!params.isEmpty()) {
237-
for (String key : params.getNames()) {
238-
builder.add(key, String.valueOf(params.get(key)));
231+
} else if (type == BodyType.JSON) {
232+
body = new JsonBody(params.getParams());
233+
} else {
234+
FormBody.Builder builder = new FormBody.Builder();
235+
if (!params.isEmpty()) {
236+
for (String key : params.getNames()) {
237+
builder.add(key, String.valueOf(params.get(key)));
238+
}
239239
}
240-
}
241-
if (mUpdateListener != null) {
242-
return new ProgressBody(builder.build(), getLifecycleOwner(), mUpdateListener);
240+
body = builder.build();
243241
}
244242

245-
return builder.build();
243+
return mUpdateListener == null ? body : new ProgressBody(body, getLifecycleOwner(), mUpdateListener);
246244
}
247245
}

0 commit comments

Comments
 (0)