This commit is contained in:
tanghc
2019-04-08 12:27:43 +08:00
parent 842a8a4a0b
commit 45ab847197
7 changed files with 16 additions and 45 deletions

View File

@@ -6,10 +6,10 @@
## sdk-java ## sdk-java
开放平台对应的sdk适用于Android。SDK依赖了三个jar包 SDK依赖了三个jar包
- okhttp.jar 用于网络请求 - okhttp.jar 用于网络请求
- fastjson-android.jar 用于json处理 - fastjson.jar 用于json处理
- commons-logging.jar 日志处理 - commons-logging.jar 日志处理
### 接口封装步骤 ### 接口封装步骤
@@ -104,9 +104,10 @@ public void testGet() {
if (response.isSuccess()) { if (response.isSuccess()) {
// 返回结果 // 返回结果
System.out.println(response); System.out.println(String.format("成功response:%s\n响应原始内容:%s",
JSON.toJSONString(response), response.getBody()));
} else { } else {
System.out.println(response); System.out.println("错误subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
} }
} }
``` ```

View File

@@ -24,7 +24,7 @@
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
<version>1.1.68.android</version> <version>1.2.56</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@@ -3,11 +3,11 @@
sdk for java sdk for java
开放平台对应的sdk适用于Android。SDK只依赖了三个jar包 SDK只依赖了三个jar包
- okhttp.jar - okhttp.jar 用于网络请求
- fastjson-android.jar - fastjson.jar 用于json处理
- commons-logging.jar - commons-logging.jar 日志处理
## 接口封装步骤 ## 接口封装步骤

View File

@@ -1,10 +1,10 @@
package com.gitee.sop.sdk.client; package com.gitee.sop.sdk.client;
import com.alibaba.fastjson.JSON;
import com.gitee.sop.sdk.common.OpenConfig; import com.gitee.sop.sdk.common.OpenConfig;
import com.gitee.sop.sdk.common.RequestForm; import com.gitee.sop.sdk.common.RequestForm;
import com.gitee.sop.sdk.common.UploadFile; import com.gitee.sop.sdk.common.UploadFile;
import com.gitee.sop.sdk.response.BaseResponse; import com.gitee.sop.sdk.response.BaseResponse;
import com.gitee.sop.sdk.util.JsonUtil;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@@ -49,7 +49,7 @@ public class OpenRequest {
result.setSubCode(HTTP_ERROR_CODE); result.setSubCode(HTTP_ERROR_CODE);
result.setSubMsg(e.getMessage()); result.setSubMsg(e.getMessage());
result.setMsg(e.getMessage()); result.setMsg(e.getMessage());
return JsonUtil.toJSONString(result); return JSON.toJSONString(result);
} }
static class ErrorResponse extends BaseResponse { static class ErrorResponse extends BaseResponse {

View File

@@ -28,8 +28,10 @@ public abstract class BaseResponse {
private String subCode; private String subCode;
@JSONField(name = "sub_msg") @JSONField(name = "sub_msg")
private String subMsg; private String subMsg;
@JSONField(serialize = false)
private String body; private String body;
@JSONField(serialize = false)
public boolean isSuccess() { public boolean isSuccess() {
return StringUtils.isEmpty(subCode); return StringUtils.isEmpty(subCode);
} }

View File

@@ -1,33 +0,0 @@
package com.gitee.sop.sdk.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class JsonUtil {
/**
* 对象转json
* @param obj
* @return
*/
public static String toJSONString(Object obj) {
if(obj == null) {
return "{}";
}
return JSON.toJSONString(obj);
}
/**
* json转对象
* @param json
* @param clazz
* @return
*/
public static <T> T parseObject(String json, Class<T> clazz) {
return JSON.parseObject(json, clazz);
}
public static JSONObject parseJSONObject(String json) {
return JSON.parseObject(json);
}
}

View File

@@ -39,7 +39,8 @@ public class SdkTest extends TestCase {
if (response.isSuccess()) { if (response.isSuccess()) {
// 返回结果 // 返回结果
System.out.println(response); System.out.println(String.format("成功response:%s\n响应原始内容:%s",
JSON.toJSONString(response), response.getBody()));
} else { } else {
System.out.println("错误subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg()); System.out.println("错误subCode:" + response.getSubCode() + ", subMsg:" + response.getSubMsg());
} }