diff --git a/README.md b/README.md
index fbcef6cb06f2b3458862c2591fb534045a0894dc..340545b09c07c84688518d6e4abfbcf1239b9483 100644
--- a/README.md
+++ b/README.md
@@ -92,12 +92,12 @@ Websocket 方法:
项目 | 最新版本 | 描述
-|-|-
-[okhttps](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps) | 3.0.1 | OkHttps 核心模块
-[okhttps-fastjson](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-fastjson) | 3.0.1 | 与 fastjson 集成
-[okhttps-gson](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-gson) | 3.0.1 | 与 gson 集成
-[okhttps-jackson](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-jackson) | 3.0.1 | 与 jackson 集成
-[okhttps-stomp](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-stomp) | 3.0.1 | 超级轻量的 Stomp 客户端
-[okhttps-xml](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-xml) | 3.0.1 | XML 解析扩展
+[okhttps](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps) | 3.0.2 | OkHttps 核心模块
+[okhttps-fastjson](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-fastjson) | 3.0.2 | 与 fastjson 集成
+[okhttps-gson](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-gson) | 3.0.2 | 与 gson 集成
+[okhttps-jackson](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-jackson) | 3.0.2 | 与 jackson 集成
+[okhttps-stomp](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-stomp) | 3.0.2 | 超级轻量的 Stomp 客户端
+[okhttps-xml](https://gitee.com/ejlchina-zhxu/okhttps/tree/dev/okhttps-xml) | 3.0.2 | XML 解析扩展
[okhttps-parallel](https://gitee.com/ejlchina-zhxu/okhttps-parallel) | 2.5.1 | 同文件多任务并行下载
## 超详细教程,请查看:https://okhttps.ejlchina.com/
diff --git a/okhttps-fastjson/README.md b/okhttps-fastjson/README.md
index ed012d57c3226343c1d685d4e0865b9f425a7d6a..da1eead4dfe2ea032800481f06ba9052400282c2 100644
--- a/okhttps-fastjson/README.md
+++ b/okhttps-fastjson/README.md
@@ -11,14 +11,14 @@
com.ejlchina
okhttps-fastjson
- 3.0.1
+ 3.0.2
```
##### Gradle
```groovy
-implementation 'com.ejlchina:okhttps-fastjson:3.0.1'
+implementation 'com.ejlchina:okhttps-fastjson:3.0.2'
```
#### 更多请参阅 [http://okhttps.ejlchina.com/](http://okhttps.ejlchina.com/)
diff --git a/okhttps-fastjson/pom.xml b/okhttps-fastjson/pom.xml
index 2a471f0bb4bd91fb5b8c18e184b39ebebbb7e6f2..0391de9337e012257f60967877528f992fd456b4 100644
--- a/okhttps-fastjson/pom.xml
+++ b/okhttps-fastjson/pom.xml
@@ -8,7 +8,7 @@
com.ejlchina
okhttps-parent
- 3.0.1
+ 3.0.2
diff --git a/okhttps-gson/README.md b/okhttps-gson/README.md
index df1d8a1f13eea14f024cf96b33166534a3e45135..9e351046cce0c16c12f52c72fb9ec5e49a475b03 100644
--- a/okhttps-gson/README.md
+++ b/okhttps-gson/README.md
@@ -11,14 +11,14 @@
com.ejlchina
okhttps-gson
- 3.0.1
+ 3.0.2
```
##### Gradle
```groovy
-implementation 'com.ejlchina:okhttps-gson:3.0.1'
+implementation 'com.ejlchina:okhttps-gson:3.0.2'
```
#### 更多请参阅 [http://okhttps.ejlchina.com/](http://okhttps.ejlchina.com/)
diff --git a/okhttps-gson/pom.xml b/okhttps-gson/pom.xml
index 7031033fc6270453ff7fed4d5f6e330f0338dd01..1f6976b28e978e3bb54c21497be5f6d21c205729 100644
--- a/okhttps-gson/pom.xml
+++ b/okhttps-gson/pom.xml
@@ -8,7 +8,7 @@
com.ejlchina
okhttps-parent
- 3.0.1
+ 3.0.2
diff --git a/okhttps-jackson/README.md b/okhttps-jackson/README.md
index f32536d92a160143c5346631be0baa6da95d810a..332cb99e0ff3b5ed599d5f641dbc030696bd81ca 100644
--- a/okhttps-jackson/README.md
+++ b/okhttps-jackson/README.md
@@ -11,14 +11,14 @@
com.ejlchina
okhttps-jackson
- 3.0.1
+ 3.0.2
```
##### Gradle
```groovy
-implementation 'com.ejlchina:okhttps-jackson:3.0.1'
+implementation 'com.ejlchina:okhttps-jackson:3.0.2'
```
#### 更多请参阅 [http://okhttps.ejlchina.com/](http://okhttps.ejlchina.com/)
diff --git a/okhttps-jackson/pom.xml b/okhttps-jackson/pom.xml
index 0ab3c512fe75772f0131b541342f0f9c539700f2..520dc52ce432f58c3364bf83dc101212dabc2c69 100644
--- a/okhttps-jackson/pom.xml
+++ b/okhttps-jackson/pom.xml
@@ -8,7 +8,7 @@
com.ejlchina
okhttps-parent
- 3.0.1
+ 3.0.2
diff --git a/okhttps-stomp/README.md b/okhttps-stomp/README.md
index fb922a1ce3b635ecfd7119c3d83901021e80095d..b35b84d182ea5af5f2104610480664be7508f867 100644
--- a/okhttps-stomp/README.md
+++ b/okhttps-stomp/README.md
@@ -11,14 +11,14 @@
com.ejlchina
okhttps-stomp
- 3.0.1
+ 3.0.2
```
##### Gradle
```groovy
-implementation 'com.ejlchina:okhttps-stomp:3.0.1'
+implementation 'com.ejlchina:okhttps-stomp:3.0.2'
```
#### 更多请参阅 [http://okhttps.ejlchina.com/v2/stomp.html](http://okhttps.ejlchina.com/v2/stomp.html)
diff --git a/okhttps-stomp/pom.xml b/okhttps-stomp/pom.xml
index c1ec443de3c50ed83d47af7f052f4761a9f5ca21..e284acf1f03229a8bc031e0a9cd80a4a543b07f9 100644
--- a/okhttps-stomp/pom.xml
+++ b/okhttps-stomp/pom.xml
@@ -8,7 +8,7 @@
com.ejlchina
okhttps-parent
- 3.0.1
+ 3.0.2
diff --git a/okhttps-test/pom.xml b/okhttps-test/pom.xml
index 491fa127b3772969d11e43b0336a9f9574bbbf59..84acb8d8f95e9fe74f11826543ce29151c4e624f 100644
--- a/okhttps-test/pom.xml
+++ b/okhttps-test/pom.xml
@@ -8,7 +8,7 @@
com.ejlchina
okhttps-parent
- 3.0.1
+ 3.0.2
diff --git a/okhttps-xml/README.md b/okhttps-xml/README.md
index af6f0d00a725031b41de5d4fc961d95b2c860fe7..a2ad34178de37e40c3138cf9e50f7e9ba1a10347 100644
--- a/okhttps-xml/README.md
+++ b/okhttps-xml/README.md
@@ -11,14 +11,14 @@
com.ejlchina
okhttps-xml
- 3.0.1
+ 3.0.2
```
##### Gradle
```groovy
-implementation 'com.ejlchina:okhttps-xml:3.0.1'
+implementation 'com.ejlchina:okhttps-xml:3.0.2'
```
#### 更多请参阅 [http://okhttps.ejlchina.com/](http://okhttps.ejlchina.com/)
diff --git a/okhttps-xml/pom.xml b/okhttps-xml/pom.xml
index 73cbcb51908004811a207697ae00a70c77c8f083..c1ee0c32cf89ab4c547d74b3ae5a4004d9aeca40 100644
--- a/okhttps-xml/pom.xml
+++ b/okhttps-xml/pom.xml
@@ -8,7 +8,7 @@
com.ejlchina
okhttps-parent
- 3.0.1
+ 3.0.2
diff --git a/okhttps/README.md b/okhttps/README.md
index b31ea1f035e9ee559abe916d28f5702d050ec9b9..3e4fd846f7d9b0af7d906f357b92292413ad29c2 100644
--- a/okhttps/README.md
+++ b/okhttps/README.md
@@ -11,14 +11,14 @@
com.ejlchina
okhttps
- 3.0.1
+ 3.0.2
```
##### Gradle
```groovy
-implementation 'com.ejlchina:okhttps:3.0.1'
+implementation 'com.ejlchina:okhttps:3.0.2'
```
#### 更多请参阅 [http://okhttps.ejlchina.com/](http://okhttps.ejlchina.com/)
diff --git a/okhttps/pom.xml b/okhttps/pom.xml
index d20588a8dcb0068cc1c6d45d2e49a58944788d80..4d736ff45adcd38d768b488f87f14db5e808d193 100644
--- a/okhttps/pom.xml
+++ b/okhttps/pom.xml
@@ -8,7 +8,7 @@
com.ejlchina
okhttps-parent
- 3.0.1
+ 3.0.2
@@ -22,6 +22,11 @@
okhttp
${okhttp.version}
+
+ com.squareup.okio
+ okio
+ ${okio.version}
+
com.squareup.okhttp3
mockwebserver
diff --git a/okhttps/src/main/java/com/ejlchina/okhttps/HttpTask.java b/okhttps/src/main/java/com/ejlchina/okhttps/HttpTask.java
index baefed63500754fc2db3d7ae9f6ad29df06b62d3..0ba132203af8b947d843d806abbd332b1a643d64 100644
--- a/okhttps/src/main/java/com/ejlchina/okhttps/HttpTask.java
+++ b/okhttps/src/main/java/com/ejlchina/okhttps/HttpTask.java
@@ -617,7 +617,8 @@ public abstract class HttpTask> implements Cancelable {
}
private RequestBody buildRequestBody() {
- if (files != null) {
+ if (bodyParams != null && OkHttps.FORM_DATA.equalsIgnoreCase(bodyType)
+ || files != null) {
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
if (bodyParams != null) {
for (String name : bodyParams.keySet()) {
@@ -626,16 +627,18 @@ public abstract class HttpTask> implements Cancelable {
builder.addPart(MultipartBody.Part.createFormData(name, null, body));
}
}
- for (String name : files.keySet()) {
- FilePara file = files.get(name);
- MediaType type = httpClient.mediaType(file.type);
- RequestBody bodyPart;
- if (file.file != null) {
- bodyPart = RequestBody.create(type, file.file);
- } else {
- bodyPart = RequestBody.create(type, file.content);
+ if (files != null) {
+ for (String name : files.keySet()) {
+ FilePara file = files.get(name);
+ MediaType type = httpClient.mediaType(file.type);
+ RequestBody bodyPart;
+ if (file.file != null) {
+ bodyPart = RequestBody.create(type, file.file);
+ } else {
+ bodyPart = RequestBody.create(type, file.content);
+ }
+ builder.addFormDataPart(name, file.fileName, bodyPart);
}
- builder.addFormDataPart(name, file.fileName, bodyPart);
}
return builder.build();
}
@@ -643,7 +646,7 @@ public abstract class HttpTask> implements Cancelable {
return toRequestBody(requestBody);
}
if (bodyParams == null) {
- return new FormBody.Builder(charset).build();
+ return emptyRequestBody();
}
if (OkHttps.FORM.equalsIgnoreCase(bodyType)) {
FormBody.Builder builder = new FormBody.Builder(charset);
@@ -656,11 +659,22 @@ public abstract class HttpTask> implements Cancelable {
return toRequestBody(bodyParams);
}
+ private RequestBody emptyRequestBody() {
+ if (OkHttps.FORM_DATA.equalsIgnoreCase(bodyType)) {
+ return new MultipartBody.Builder().setType(MultipartBody.FORM).build();
+ }
+ return RequestBody.create(mediaType(), new byte[]{});
+ }
+
+ private MediaType mediaType() {
+ String mediaType = httpClient.executor().doMsgConvert(bodyType, null).contentType;
+ return MediaType.parse(mediaType + "; charset=" + charset.name());
+ }
+
private RequestBody toRequestBody(Object object) {
if (object instanceof byte[] || object instanceof String) {
- String mediaType = httpClient.executor().doMsgConvert(bodyType, null).contentType;
byte[] body = object instanceof byte[] ? (byte[]) object : ((String) object).getBytes(charset);
- return RequestBody.create(MediaType.parse(mediaType + "; charset=" + charset.name()), body);
+ return RequestBody.create(mediaType(), body);
}
TaskExecutor.Data data = httpClient.executor().doMsgConvert(bodyType, c -> c.serialize(object, charset));
return RequestBody.create(MediaType.parse(data.contentType + "; charset=" + charset.name()), data.data);
diff --git a/okhttps/src/main/java/com/ejlchina/okhttps/OkHttps.java b/okhttps/src/main/java/com/ejlchina/okhttps/OkHttps.java
index ba95fe8c662c680340e0d462546f7d1a1f7bba2b..2e2219c3247b4edc182e59f33bbe4230f24bdb89 100644
--- a/okhttps/src/main/java/com/ejlchina/okhttps/OkHttps.java
+++ b/okhttps/src/main/java/com/ejlchina/okhttps/OkHttps.java
@@ -12,7 +12,10 @@ import okhttp3.WebSocketListener;
*/
public final class OkHttps {
+ // 使用 x-www-form-urlencoded 表单格式
public static final String FORM = "form";
+ // 使用 form-data 表单格式(一般上传文件时使用)
+ public static final String FORM_DATA = "form-data";
public static final String JSON = "json";
public static final String XML = "xml";
public static final String PROTOBUF = "protobuf";
diff --git a/okhttps/src/test/java/com/ejlchina/test/SimpleTests.java b/okhttps/src/test/java/com/ejlchina/test/SimpleTests.java
index 177b3cabfeb8ef3949919d0199ed7b6da2689cef..5c0645ac3f32ac020a83da9aaff3aaa53bdd2566 100644
--- a/okhttps/src/test/java/com/ejlchina/test/SimpleTests.java
+++ b/okhttps/src/test/java/com/ejlchina/test/SimpleTests.java
@@ -1,7 +1,11 @@
package com.ejlchina.test;
import com.ejlchina.okhttps.HTTP;
+import com.ejlchina.okhttps.OkHttps;
+import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
+import org.junit.Assert;
+import org.junit.Test;
public class SimpleTests extends BaseTest {
@@ -12,22 +16,31 @@ public class SimpleTests extends BaseTest {
.baseUrl("http://" + server.getHostName() + ":" + server.getPort())
.build();
+ @Test
+ public void testSyncPost() throws InterruptedException {
+ http.async("/").bodyType(OkHttps.JSON).post();
+ Assert.assertEquals(server.takeRequest().getHeader("Content-Type"), "application/json; charset=UTF-8");
+ http.async("/").bodyType(OkHttps.FORM).post();
+ Assert.assertEquals(server.takeRequest().getHeader("Content-Type"), "application/x-www-form-urlencoded; charset=UTF-8");
+ }
+
+
/**
* 同步请求示例
* 同步请求直接得到结果,无需设置回调
*/
-// @Test
-// public void testSyncToBean() {
-// server.enqueue(new MockResponse().setBody("Hello World!"));
-//
-// JsonObj hello = http.sync("/users") // http://localhost:8080/users
-// .get() // GET请求
-// .getBody() // 获取响应报文体
-// .toJsonObj(); // 得到目标数据
-//
-// System.out.println("hello = " + hello);
-// }
-
+ @Test
+ public void testSyncToBean() {
+ server.enqueue(new MockResponse().setBody("Hello World!"));
+
+ String hello = http.sync("/users") // http://localhost:8080/users
+ .get() // GET请求
+ .getBody() // 获取响应报文体
+ .toString(); // 得到目标数据
+
+ Assert.assertEquals(hello, "Hello World!");
+ }
+
/**
* 同步请求示例
* 同步请求直接得到结果,无需设置回调
@@ -38,12 +51,12 @@ public class SimpleTests extends BaseTest {
// User u2 = new User(2, "Tom");
// List list = Arrays.asList(u1, u2);
// server.enqueue(new MockResponse().setBody(JSON.toJSONString(list)));
-//
+//
// List users = http.sync("/users") // http://localhost:8080/users
// .get() // GET请求
// .getBody() // 获取响应报文体
// .toList(User.class); // 得到目标数据
-//
+//
// assertEquals(u1, users.get(0));
// assertEquals(u2, users.get(1));
// }
@@ -98,7 +111,6 @@ public class SimpleTests extends BaseTest {
// System.out.println("size = " + (size / 1024) + " KB");
// }
-
}
diff --git a/pom.xml b/pom.xml
index bdb35d348dd599cb196787d438c4b3c6ac1eab8f..7e9be2f31e9cb786fc2be1c0a365507d9999c328 100644
--- a/pom.xml
+++ b/pom.xml
@@ -3,7 +3,7 @@
com.ejlchina
okhttps-parent
- 3.0.1
+ 3.0.2
OkHttps
pom
@@ -20,11 +20,9 @@
1.8
- 1.0.0
+ 1.0.1
3.14.9
- 2.8.6
- 1.2.75
- 2.11.3
+ 1.17.5
4.13.1
UTF-8
https://oss.sonatype.org/content/repositories/snapshots/