Skip to content

Commit

Permalink
修复错误返回若不是json格式时的崩溃 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Yin committed Jan 7, 2021
1 parent 56c073a commit cfa171b
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ufile-sample-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-client-java</artifactId>
<version>2.6.2</version>
<version>2.6.4</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion ufile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile</artifactId>
<packaging>pom</packaging>
<version>2.6.3</version>
<version>2.6.4</version>

<modules>
<module>ufile-core</module>
Expand Down
Binary file modified ufile/ufile-client-java/apidocs.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions ufile/ufile-client-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<parent>
<artifactId>ufile</artifactId>
<groupId>cn.ucloud.ufile</groupId>
<version>2.6.3</version>
<version>2.6.4</version>
</parent>

<artifactId>ufile-client-java</artifactId>
<version>2.6.3</version>
<version>2.6.4</version>

<dependencies>
<dependency>
Expand All @@ -26,7 +26,7 @@
<dependency>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile-core</artifactId>
<version>2.6.3</version>
<version>2.6.4</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ public UfileErrorBean parseErrorResponse(Response response) throws UfileClientEx
errorBean = new Gson().fromJson((content == null || content.length() == 0) ? "{}" : content, UfileErrorBean.class);
} catch (Exception e) {
errorBean = new UfileErrorBean();
errorBean.setErrMsg(content);
}
errorBean.setResponseCode(response.code());
errorBean.setxSessionId(response.header("X-SessionId"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ public UfileErrorBean parseErrorResponse(Response response) throws UfileClientEx
errorBean = new Gson().fromJson((content == null || content.length() == 0) ? "{}" : content, UfileErrorBean.class);
} catch (Exception e) {
errorBean = new UfileErrorBean();
errorBean.setErrMsg(content);
}
errorBean.setResponseCode(response.code());
errorBean.setxSessionId(response.header("X-SessionId"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public UfileErrorBean parseErrorResponse(Response response) throws UfileClientEx
errorBean = new Gson().fromJson((content == null || content.length() == 0) ? "{}" : content, UfileErrorBean.class);
} catch (Exception e) {
errorBean = new UfileErrorBean();
errorBean.setErrMsg(content);
}
errorBean.setResponseCode(response.code());
errorBean.setxSessionId(response.header("X-SessionId"));
Expand Down
Binary file modified ufile/ufile-core/apidocs.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions ufile/ufile-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<parent>
<groupId>cn.ucloud.ufile</groupId>
<artifactId>ufile</artifactId>
<version>2.6.3</version>
<version>2.6.4</version>
</parent>

<artifactId>ufile-core</artifactId>
<version>2.6.3</version>
<version>2.6.4</version>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @date: 2018/11/22 15:00
*/
public class UfileConstants {
public static final String SDK_VERSION = "2.6.3";
public static final String SDK_VERSION = "2.6.4";
/**
* 默认分片大小(4MB)
*/
Expand Down
11 changes: 9 additions & 2 deletions ufile/ufile-core/src/main/java/cn/ucloud/ufile/api/UfileApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cn.ucloud.ufile.http.HttpClient;
import cn.ucloud.ufile.http.response.ResponseParser;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -288,8 +289,14 @@ public T parseHttpResponse(Response response) throws UfileClientException, Ufile
public UfileErrorBean parseErrorResponse(Response response) throws UfileClientException {
try {
String content = response.body().string();
content = (content == null || content.length() == 0) ? "{}" : content;
UfileErrorBean errorBean = new Gson().fromJson(content, UfileErrorBean.class);
content = (content == null || content.length() == 0) ? "" : content;
UfileErrorBean errorBean = null;
try {
errorBean = new Gson().fromJson(content, UfileErrorBean.class);
} catch (JsonParseException e) {
errorBean = new UfileErrorBean();
errorBean.setErrMsg(content);
}
errorBean.setResponseCode(response.code());
errorBean.setxSessionId(response.header("X-SessionId"));
return errorBean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@ public Response intercept(Chain chain) throws IOException {
JLog.T(TAG, "[response-code]:" + response.code());
JLog.T(TAG, "[response-headers]:" + response.headers().toString());

/* 获得返回的body,注意此处不要使用responseBody.string()获取返回数据,原因在于这个方法会消耗返回结果的数据(buffer) */
// ResponseBody responseBody = response.body();
if (JLog.SHOW_TEST) {
/* 获得返回的body,注意此处不要使用responseBody.string()获取返回数据,原因在于这个方法会消耗返回结果的数据(buffer) */
ResponseBody responseBody = response.body();

/* 为了不消耗buffer,我们这里使用source先获得buffer对象,然后clone()后使用 */
// BufferedSource source = responseBody.source();
// source.request(Long.MAX_VALUE); // Buffer the entire body.
// /* 获得返回的数据 */
// Buffer buffer = source.buffer();
// if (buffer.size() < 1024)
// /* 使用前clone() 下,避免直接消耗 */
// JLog.T(TAG, "[response-body]:" + buffer.clone().readString(Charset.forName("UTF-8")));
/* 为了不消耗buffer,我们这里使用source先获得buffer对象,然后clone()后使用 */
BufferedSource source = responseBody.source();
source.request(Long.MAX_VALUE); // Buffer the entire body.
/* 获得返回的数据 */
Buffer buffer = source.buffer();
if (buffer.size() < 2 << 10)
/* 使用前clone() 下,避免直接消耗 */
JLog.T(TAG, "[response-body]:" + buffer.clone().readString(Charset.forName("UTF-8")));
}

return response;
}
Expand Down

0 comments on commit cfa171b

Please sign in to comment.