Skip to content

Commit

Permalink
兼容 MultipartBody.Builder 在 build 时参数为空会报错的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed May 13, 2021
1 parent 355853e commit f90c894
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
Binary file modified EasyHttp.apk
Binary file not shown.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@

```groovy
buildscript {
......
repositories {
maven { url 'https://jitpack.io' }
}
}
allprojects {
repositories {
// JitPack 远程仓库:https://jitpack.io
maven { url 'https://jitpack.io' }
}
}
Expand All @@ -51,7 +52,7 @@ android {
dependencies {
// 网络请求框架:https://github.com/getActivity/EasyHttp
implementation 'com.github.getActivity:EasyHttp:9.5'
implementation 'com.github.getActivity:EasyHttp:9.6'
// OkHttp 框架:https://github.com/square/okhttp
// noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:3.12.13'
Expand All @@ -64,7 +65,7 @@ dependencies {

| 功能或细节 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
| :----: | :------: | :-----: | :-----: |
| 对应版本 | 9.5 | 2.9.0 | 3.0.4 |
| 对应版本 | 9.6 | 2.9.0 | 3.0.4 |
| **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) |
| minSdk 要求 | API 14+ | API 21+ | API 14+ |
| 配置多域名 ||||
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId 'com.hjq.easy.demo'
minSdkVersion 16
targetSdkVersion 30
versionCode 95
versionName '9.5'
versionCode 96
versionName '9.6'
}

// 支持 JDK 1.8
Expand Down Expand Up @@ -49,7 +49,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'

// 吐司框架:https://github.com/getActivity/ToastUtils
implementation 'com.github.getActivity:ToastUtils:9.1'
implementation 'com.github.getActivity:ToastUtils:9.2'

// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:10.8'
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 95
versionName "9.5"
versionCode 96
versionName "9.6"
}

// 使用 JDK 1.8
Expand Down
36 changes: 17 additions & 19 deletions library/src/main/java/com/hjq/http/request/BodyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ public T request(OnHttpListener<?> listener) {
* 组装 RequestBody 对象
*/
private RequestBody createBody(HttpParams params, BodyType type) {
RequestBody body;

if (params.isMultipart() && !params.isEmpty()) {
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
Expand Down Expand Up @@ -219,29 +221,25 @@ private RequestBody createBody(HttpParams params, BodyType type) {
builder.addFormDataPart(key, String.valueOf(object));
}

if (mUpdateListener != null) {
return new ProgressBody(builder.build(), getLifecycleOwner(), mUpdateListener);
try {
body = builder.build();
} catch (IllegalStateException ignored) {
// 如果参数为空则会抛出异常:Multipart body must have at least one part.
body = new FormBody.Builder().build();
}
return builder.build();
}

if (type == BodyType.JSON) {
if (mUpdateListener != null) {
return new ProgressBody(new JsonBody(params.getParams()), getLifecycleOwner(), mUpdateListener);
}
return new JsonBody(params.getParams());
}

FormBody.Builder builder = new FormBody.Builder();
if (!params.isEmpty()) {
for (String key : params.getNames()) {
builder.add(key, String.valueOf(params.get(key)));
} else if (type == BodyType.JSON) {
body = new JsonBody(params.getParams());
} else {
FormBody.Builder builder = new FormBody.Builder();
if (!params.isEmpty()) {
for (String key : params.getNames()) {
builder.add(key, String.valueOf(params.get(key)));
}
}
}
if (mUpdateListener != null) {
return new ProgressBody(builder.build(), getLifecycleOwner(), mUpdateListener);
body = builder.build();
}

return builder.build();
return mUpdateListener == null ? body : new ProgressBody(body, getLifecycleOwner(), mUpdateListener);
}
}

0 comments on commit f90c894

Please sign in to comment.