Skip to content

Commit

Permalink
修复上传 Json 包含中文字符时字节长度计算不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Nov 7, 2020
1 parent fa6588d commit e163f27
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
Binary file modified EasyHttp.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ android {
dependencies {
// 网络请求框架:https://github.com/getActivity/EasyHttp
implementation 'com.hjq:http:8.8'
implementation 'com.hjq:http:8.9'
// OkHttp 框架:https://github.com/square/okhttp
// noinspection GradleDependency
implementation 'com.squareup.okhttp3:okhttp:3.12.12'
Expand All @@ -46,7 +46,7 @@ dependencies {

| 功能 | [EasyHttp](https://github.com/getActivity/EasyHttp) | [Retrofit](https://github.com/square/retrofit) | [OkGo](https://github.com/jeasonlzy/okhttp-OkGo) |
| :----: | :------: | :-----: | :-----: |
| 对应版本 | 8.8 | 2.9.0 | 3.0.4 |
| 对应版本 | 8.9 | 2.9.0 | 3.0.4 |
| 动态 Host | 支持 | 不支持 | 支持 |
| 全局参数 | 支持 | 不支持 | 支持 |
| 超时重试 | 支持 | 不支持 | 支持 |
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId 'com.hjq.http.demo'
minSdkVersion 16
targetSdkVersion 30
versionCode 88
versionName '8.8'
versionCode 89
versionName '8.9'
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 88
versionName "8.8"
versionCode 89
versionName "8.9"
}
}

Expand All @@ -30,7 +30,7 @@ publish {
userOrg = 'getactivity'
groupId = 'com.hjq'
artifactId = 'http'
version = '8.8'
version = '8.9'
description = 'Easy-to-use network request framework'
website = "https://github.com/getActivity/EasyHttp"
}
Expand Down
12 changes: 8 additions & 4 deletions library/src/main/java/com/hjq/http/body/JsonBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
*/
public final class JsonBody extends RequestBody {

/** Json 文本数据 */
/** Json 数据 */
private final String mJson;
/** 字节数组 */
private final byte[] mBytes;

public JsonBody(Map map) {
this(new JSONObject(map));
}

public JsonBody(JSONObject jsonObject) {
mJson = jsonObject.toString();
mBytes = mJson.getBytes();
}

public JsonBody(List list) {
Expand All @@ -38,6 +41,7 @@ public JsonBody(List list) {

public JsonBody(JSONArray jsonArray) {
mJson = jsonArray.toString();
mBytes = mJson.getBytes();
}

@Override
Expand All @@ -47,13 +51,13 @@ public MediaType contentType() {

@Override
public long contentLength() {
return mJson.length();
// 需要注意:这里需要用字节数组的长度来计算
return mBytes.length;
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
byte[] bytes = mJson.getBytes();
sink.write(bytes, 0, bytes.length);
sink.write(mBytes, 0, mBytes.length);
}

@NonNull
Expand Down
11 changes: 8 additions & 3 deletions library/src/main/java/com/hjq/http/body/StringBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,19 @@
*/
public final class StringBody extends RequestBody {

/** 字符串数据 */
private final String mText;

/** 字节数组 */
private final byte[] mBytes;

public StringBody() {
this("");
}

public StringBody(String text) {
mText = text;
mBytes = mText.getBytes();
}

@Override
Expand All @@ -33,13 +38,13 @@ public MediaType contentType() {

@Override
public long contentLength() {
return mText.length();
// 需要注意:这里需要用字节数组的长度来计算
return mBytes.length;
}

@Override
public void writeTo(BufferedSink sink) throws IOException {
byte[] bytes = mText.getBytes();
sink.write(bytes, 0, bytes.length);
sink.write(mBytes, 0, mBytes.length);
}

@NonNull
Expand Down
9 changes: 9 additions & 0 deletions library/src/main/java/com/hjq/http/request/BodyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,23 @@ public BodyRequest(LifecycleOwner lifecycleOwner) {
}

public T body(Map map) {
if (map == null) {
return (T) this;
}
return body(new JsonBody(map));
}

public T body(List list) {
if (list == null) {
return (T) this;
}
return body(new JsonBody(list));
}

public T body(String text) {
if (text == null) {
return (T) this;
}
return body(new StringBody(text));
}

Expand Down

0 comments on commit e163f27

Please sign in to comment.