Skip to content

Commit e163f27

Browse files
committed
修复上传 Json 包含中文字符时字节长度计算不正确的问题
1 parent fa6588d commit e163f27

File tree

7 files changed

+32
-14
lines changed

7 files changed

+32
-14
lines changed

EasyHttp.apk

37.4 KB
Binary file not shown.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ android {
3333
3434
dependencies {
3535
// 网络请求框架:https://github.com/getActivity/EasyHttp
36-
implementation 'com.hjq:http:8.8'
36+
implementation 'com.hjq:http:8.9'
3737
// OkHttp 框架:https://github.com/square/okhttp
3838
// noinspection GradleDependency
3939
implementation 'com.squareup.okhttp3:okhttp:3.12.12'
@@ -46,7 +46,7 @@ dependencies {
4646

4747
| 功能 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
4848
| :----: | :------: | :-----: | :-----: |
49-
| 对应版本 | 8.8 | 2.9.0 | 3.0.4 |
49+
| 对应版本 | 8.9 | 2.9.0 | 3.0.4 |
5050
| 动态 Host | 支持 | 不支持 | 支持 |
5151
| 全局参数 | 支持 | 不支持 | 支持 |
5252
| 超时重试 | 支持 | 不支持 | 支持 |

app/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ android {
1717
applicationId 'com.hjq.http.demo'
1818
minSdkVersion 16
1919
targetSdkVersion 30
20-
versionCode 88
21-
versionName '8.8'
20+
versionCode 89
21+
versionName '8.9'
2222
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
2323
}
2424

library/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212

1313
defaultConfig {
1414
minSdkVersion 14
15-
versionCode 88
16-
versionName "8.8"
15+
versionCode 89
16+
versionName "8.9"
1717
}
1818
}
1919

@@ -30,7 +30,7 @@ publish {
3030
userOrg = 'getactivity'
3131
groupId = 'com.hjq'
3232
artifactId = 'http'
33-
version = '8.8'
33+
version = '8.9'
3434
description = 'Easy-to-use network request framework'
3535
website = "https://github.com/getActivity/EasyHttp"
3636
}

library/src/main/java/com/hjq/http/body/JsonBody.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@
2121
*/
2222
public final class JsonBody extends RequestBody {
2323

24-
/** Json 文本数据 */
24+
/** Json 数据 */
2525
private final String mJson;
26+
/** 字节数组 */
27+
private final byte[] mBytes;
2628

2729
public JsonBody(Map map) {
2830
this(new JSONObject(map));
2931
}
3032

3133
public JsonBody(JSONObject jsonObject) {
3234
mJson = jsonObject.toString();
35+
mBytes = mJson.getBytes();
3336
}
3437

3538
public JsonBody(List list) {
@@ -38,6 +41,7 @@ public JsonBody(List list) {
3841

3942
public JsonBody(JSONArray jsonArray) {
4043
mJson = jsonArray.toString();
44+
mBytes = mJson.getBytes();
4145
}
4246

4347
@Override
@@ -47,13 +51,13 @@ public MediaType contentType() {
4751

4852
@Override
4953
public long contentLength() {
50-
return mJson.length();
54+
// 需要注意:这里需要用字节数组的长度来计算
55+
return mBytes.length;
5156
}
5257

5358
@Override
5459
public void writeTo(BufferedSink sink) throws IOException {
55-
byte[] bytes = mJson.getBytes();
56-
sink.write(bytes, 0, bytes.length);
60+
sink.write(mBytes, 0, mBytes.length);
5761
}
5862

5963
@NonNull

library/src/main/java/com/hjq/http/body/StringBody.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@
1616
*/
1717
public final class StringBody extends RequestBody {
1818

19+
/** 字符串数据 */
1920
private final String mText;
2021

22+
/** 字节数组 */
23+
private final byte[] mBytes;
24+
2125
public StringBody() {
2226
this("");
2327
}
2428

2529
public StringBody(String text) {
2630
mText = text;
31+
mBytes = mText.getBytes();
2732
}
2833

2934
@Override
@@ -33,13 +38,13 @@ public MediaType contentType() {
3338

3439
@Override
3540
public long contentLength() {
36-
return mText.length();
41+
// 需要注意:这里需要用字节数组的长度来计算
42+
return mBytes.length;
3743
}
3844

3945
@Override
4046
public void writeTo(BufferedSink sink) throws IOException {
41-
byte[] bytes = mText.getBytes();
42-
sink.write(bytes, 0, bytes.length);
47+
sink.write(mBytes, 0, mBytes.length);
4348
}
4449

4550
@NonNull

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

+9
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,23 @@ public BodyRequest(LifecycleOwner lifecycleOwner) {
4343
}
4444

4545
public T body(Map map) {
46+
if (map == null) {
47+
return (T) this;
48+
}
4649
return body(new JsonBody(map));
4750
}
4851

4952
public T body(List list) {
53+
if (list == null) {
54+
return (T) this;
55+
}
5056
return body(new JsonBody(list));
5157
}
5258

5359
public T body(String text) {
60+
if (text == null) {
61+
return (T) this;
62+
}
5463
return body(new StringBody(text));
5564
}
5665

0 commit comments

Comments
 (0)