diff --git a/sop-sdk/sdk-java/pom.xml b/sop-sdk/sdk-java/pom.xml
index 84d935a1..4cb072ab 100644
--- a/sop-sdk/sdk-java/pom.xml
+++ b/sop-sdk/sdk-java/pom.xml
@@ -17,7 +17,7 @@
com.squareup.okhttp3
okhttp
- 3.10.0
+ 3.14.0
diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenClient.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenClient.java
index 5e992c5b..51b3a856 100644
--- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenClient.java
+++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenClient.java
@@ -27,27 +27,25 @@ public class OpenClient {
private static final char DOT = '.';
private static final char UNDERLINE = '_';
- public static final String GATEWAY_CODE_NAME = "code";
- public static final String GATEWAY_MSG_NAME = "msg";
public static final String DATA_SUFFIX = "_response";
private String url;
- private String appKey;
+ private String appId;
private String privateKey;
private OpenConfig openConfig;
private OpenRequest openRequest;
- public OpenClient(String url, String appKey, String privateKey) {
- this(url, appKey, privateKey, DEFAULT_CONFIG);
+ public OpenClient(String url, String appId, String privateKey) {
+ this(url, appId, privateKey, DEFAULT_CONFIG);
}
- public OpenClient(String url, String appKey, String privateKey, OpenConfig openConfig) {
+ public OpenClient(String url, String appId, String privateKey, OpenConfig openConfig) {
if (openConfig == null) {
throw new IllegalArgumentException("openConfig不能为null");
}
this.url = url;
- this.appKey = appKey;
+ this.appId = appId;
this.privateKey = privateKey;
this.openConfig = openConfig;
@@ -74,13 +72,13 @@ public class OpenClient {
* @return 返回Response
*/
public T execute(BaseRequest request, String accessToken) {
- RequestForm requestForm = request.createRequestForm();
+ RequestForm requestForm = request.createRequestForm(this.openConfig);
// 表单数据
Map form = requestForm.getForm();
if (accessToken != null) {
form.put(this.openConfig.getAccessTokenName(), accessToken);
}
- form.put(this.openConfig.getAppKeyName(), this.appKey);
+ form.put(this.openConfig.getAppKeyName(), this.appId);
String content = SopSignature.getSignContent(form);
String sign = null;
@@ -106,7 +104,7 @@ public class OpenClient {
}
protected String doExecute(String url, RequestForm requestForm, Map header) {
- return openRequest.request(this.url, requestForm, header);
+ return openRequest.request(url, requestForm, header);
}
protected T parseResponse(String resp, BaseRequest request) {
diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java
index 6ba70b70..f5d1b4ef 100644
--- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java
+++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenHttp.java
@@ -4,6 +4,7 @@ import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.UploadFile;
import okhttp3.Cookie;
import okhttp3.CookieJar;
+import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
@@ -13,6 +14,7 @@ import okhttp3.RequestBody;
import okhttp3.Response;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -38,6 +40,7 @@ public class OpenHttp {
httpClient = new OkHttpClient.Builder()
.connectTimeout(openConfig.getConnectTimeoutSeconds(), TimeUnit.SECONDS) // 设置链接超时时间,默认10秒
.readTimeout(openConfig.getReadTimeoutSeconds(), TimeUnit.SECONDS)
+ .writeTimeout(openConfig.getWriteTimeoutSeconds(), TimeUnit.SECONDS)
.cookieJar(new CookieJar() {
public void saveFromResponse(HttpUrl httpUrl, List list) {
cookieStore.put(httpUrl.host(), list);
@@ -69,23 +72,35 @@ public class OpenHttp {
}
/**
- * 提交json字符串到请求体
+ * 提交表单
*
* @param url
- * @param json
+ * @param form
* @param header header内容
* @return
* @throws IOException
*/
- public String postJsonBody(String url, String json, Map header) throws IOException {
- RequestBody body = RequestBody.create(JSON, json);
- Request.Builder builder = new Request.Builder().url(url).post(body);
+ public String postFormBody(String url, Map form, Map header) throws IOException {
+ FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8);
+ for (Map.Entry entry : form.entrySet()) {
+ paramBuilder.add(entry.getKey(), entry.getValue());
+ }
+ FormBody formBody = paramBuilder.build();
+ Request.Builder requestBuilder = new Request.Builder()
+ .url(url)
+ .post(formBody);
// 添加header
- addHeader(builder, header);
-
- Request request = builder.build();
- Response response = httpClient.newCall(request).execute();
- return response.body().string();
+ addHeader(requestBuilder, header);
+
+ Request request = requestBuilder.build();
+ Response response = httpClient
+ .newCall(request)
+ .execute();
+ try {
+ return response.body().string();
+ } finally {
+ response.close();
+ }
}
/**
@@ -125,7 +140,11 @@ public class OpenHttp {
Request request = builder.build();
Response response = httpClient.newCall(request).execute();
- return response.body().string();
+ try {
+ return response.body().string();
+ } finally {
+ response.close();
+ }
}
private void addHeader(Request.Builder builder, Map header) {
diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java
index 590e3146..27e62cc6 100644
--- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java
+++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/client/OpenRequest.java
@@ -36,7 +36,7 @@ public class OpenRequest {
if (files != null && files.size() > 0) {
return openHttp.postFile(url, form, header, files);
} else {
- return openHttp.postJsonBody(url, JsonUtil.toJSONString(form), header);
+ return openHttp.postFormBody(url, form, header);
}
} catch (IOException e) {
return this.causeException(e);
diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/OpenConfig.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/OpenConfig.java
index 6d1e64aa..08164022 100644
--- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/OpenConfig.java
+++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/common/OpenConfig.java
@@ -12,9 +12,11 @@ public class OpenConfig {
/** 默认版本号 */
private String defaultVersion = SdkConfig.DEFAULT_VERSION;
/** 接口属性名 */
- private String apiName = "method";
+ private String methodName = "method";
/** 版本号名称 */
private String versionName = "version";
+ /** 编码名称 */
+ private String charsetName = "charset";
/** appKey名称 */
private String appKeyName = "app_id";
/** data名称 */
@@ -25,9 +27,11 @@ public class OpenConfig {
private String timestampPattern = "yyyy-MM-dd HH:mm:ss";
/** 签名串名称 */
private String signName = "sign";
+ /** 签名类型名称 */
+ private String signTypeName = "sign_type";
/** 格式化名称 */
private String formatName = "format";
- /** 格式类型 */
+ /** 格式类型名称 */
private String formatType = "json";
/** accessToken名称 */
private String accessTokenName = "app_auth_token";
@@ -39,4 +43,6 @@ public class OpenConfig {
private int connectTimeoutSeconds = 10;
/** http读取超时时间 */
private int readTimeoutSeconds = 10;
+ /** http写超时时间 */
+ private int writeTimeoutSeconds = 10;
}
diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java
index dcbfd443..58060e5c 100644
--- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java
+++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/BaseRequest.java
@@ -1,13 +1,12 @@
package com.gitee.sop.sdk.request;
import com.alibaba.fastjson.JSON;
+import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm;
import com.gitee.sop.sdk.common.SdkConfig;
import com.gitee.sop.sdk.common.UploadFile;
import com.gitee.sop.sdk.response.BaseResponse;
import com.gitee.sop.sdk.util.ClassUtil;
-import lombok.Getter;
-import lombok.Setter;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -55,9 +54,16 @@ public abstract class BaseRequest {
@SuppressWarnings("unchecked")
public BaseRequest() {
- this.method = this.method();
- this.version = this.version();
+ this.setMethodVersion(this.method(), this.version());
+ }
+
+ protected BaseRequest(String method, String version) {
+ this.setMethodVersion(method, version);
+ }
+ private void setMethodVersion(String method, String version) {
+ this.method = method;
+ this.version = version == null ? SdkConfig.DEFAULT_VERSION : version;
this.responseClass = (Class) ClassUtil.getSuperClassGenricType(this.getClass(), 0);
}
@@ -65,20 +71,20 @@ public abstract class BaseRequest {
return SdkConfig.DEFAULT_VERSION;
}
- public RequestForm createRequestForm() {
+ public RequestForm createRequestForm(OpenConfig openConfig) {
// 公共请求参数
Map params = new HashMap();
- params.put("method", this.method);
- params.put("format", this.format);
- params.put("charset", this.charset);
- params.put("sign_type", this.signType);
- params.put("timestamp", this.timestamp);
- params.put("version", this.version);
+ params.put(openConfig.getMethodName(), this.method);
+ params.put(openConfig.getFormatName(), this.format);
+ params.put(openConfig.getCharsetName(), this.charset);
+ params.put(openConfig.getSignTypeName(), this.signType);
+ params.put(openConfig.getTimestampName(), this.timestamp);
+ params.put(openConfig.getVersionName(), this.version);
// 业务参数
String biz_content = buildBizContent();
- params.put("biz_content", biz_content);
+ params.put(openConfig.getDataName(), biz_content);
RequestForm requestForm = new RequestForm(params);
requestForm.setFiles(this.files);
@@ -97,10 +103,6 @@ public abstract class BaseRequest {
return method;
}
- protected void setMethod(String method) {
- this.method = method;
- }
-
public void setVersion(String version) {
this.version = version;
}
diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/CommonRequest.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/CommonRequest.java
index 8373e862..7cc95dac 100644
--- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/CommonRequest.java
+++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/request/CommonRequest.java
@@ -8,12 +8,11 @@ import com.gitee.sop.sdk.response.CommonResponse;
public class CommonRequest extends BaseRequest {
public CommonRequest(String method) {
- this.setMethod(method);
+ super(method, null);
}
public CommonRequest(String method, String version) {
- this.setMethod(method);
- this.setVersion(version);
+ super(method, version);
}
@Override
diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/CommonResponse.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/CommonResponse.java
index d0b62675..4c001434 100644
--- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/CommonResponse.java
+++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/CommonResponse.java
@@ -1,7 +1,5 @@
package com.gitee.sop.sdk.response;
-import java.util.Map;
-
/**
* @author tanghc
*/
diff --git a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/GetStoryResponse.java b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/GetStoryResponse.java
index 524b96e2..69c8b959 100644
--- a/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/GetStoryResponse.java
+++ b/sop-sdk/sdk-java/src/main/java/com/gitee/sop/sdk/response/GetStoryResponse.java
@@ -8,5 +8,5 @@ import java.util.Date;
public class GetStoryResponse extends BaseResponse {
private Long id;
private String name;
- private Date gmtCreate;
+ private Date gmt_create;
}
diff --git a/sop-sdk/sdk-java/src/test/java/com/gitee/sop/sdk/SdkTest.java b/sop-sdk/sdk-java/src/test/java/com/gitee/sop/sdk/SdkTest.java
index 04b49ece..09ecc29e 100644
--- a/sop-sdk/sdk-java/src/test/java/com/gitee/sop/sdk/SdkTest.java
+++ b/sop-sdk/sdk-java/src/test/java/com/gitee/sop/sdk/SdkTest.java
@@ -41,7 +41,7 @@ public class SdkTest extends TestCase {
// 返回结果
System.out.println(response);
} else {
- System.out.println(response);
+ System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
}
}
@@ -65,7 +65,7 @@ public class SdkTest extends TestCase {
JSONObject jsonObject = JSON.parseObject(body);
System.out.println(jsonObject);
} else {
- System.out.println(response);
+ System.out.println("错误,subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
}
}