Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
7dfab7fb
mica
项目概览
mica
/
mica
通知
10
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
7dfab7fb
编写于
9月 29, 2019
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
http close ResponseBody。
上级
3e7c28a9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
31 addition
and
11 deletion
+31
-11
mica-http/src/main/java/net/dreamlu/mica/http/HttpResponse.java
...ttp/src/main/java/net/dreamlu/mica/http/HttpResponse.java
+16
-8
mica-http/src/main/java/net/dreamlu/mica/http/ResponseSpec.java
...ttp/src/main/java/net/dreamlu/mica/http/ResponseSpec.java
+8
-0
mica-http/src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
...src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
+7
-3
未找到文件。
mica-http/src/main/java/net/dreamlu/mica/http/HttpResponse.java
浏览文件 @
7dfab7fb
...
...
@@ -24,6 +24,7 @@ import net.dreamlu.mica.core.utils.JsonUtil;
import
okhttp3.*
;
import
okhttp3.internal.Util
;
import
javax.annotation.Nonnull
;
import
javax.annotation.Nullable
;
import
java.io.File
;
import
java.io.IOException
;
...
...
@@ -41,12 +42,10 @@ import java.util.Map;
public
class
HttpResponse
implements
ResponseSpec
{
private
final
Request
request
;
private
final
Response
response
;
private
final
ResponseBody
body
;
HttpResponse
(
final
Response
response
)
{
this
.
request
=
response
.
request
();
this
.
response
=
response
;
this
.
body
=
ifNullBodyToEmpty
(
response
.
body
());
}
@Override
...
...
@@ -90,14 +89,15 @@ public class HttpResponse implements ResponseSpec {
return
this
.
response
;
}
@Nonnull
@Override
public
ResponseBody
rawBody
()
{
return
this
.
body
;
return
ifNullBodyToEmpty
(
this
.
response
.
body
())
;
}
@Override
public
String
asString
()
{
try
{
try
(
ResponseBody
body
=
rawBody
())
{
return
body
.
string
();
}
catch
(
IOException
e
)
{
throw
Exceptions
.
unchecked
(
e
);
...
...
@@ -106,7 +106,7 @@ public class HttpResponse implements ResponseSpec {
@Override
public
byte
[]
asBytes
()
{
try
{
try
(
ResponseBody
body
=
rawBody
())
{
return
body
.
bytes
();
}
catch
(
IOException
e
)
{
throw
Exceptions
.
unchecked
(
e
);
...
...
@@ -115,7 +115,10 @@ public class HttpResponse implements ResponseSpec {
@Override
public
<
T
>
T
onStream
(
CheckedFunction
<
InputStream
,
T
>
function
)
{
try
(
InputStream
input
=
body
.
byteStream
())
{
try
(
ResponseBody
body
=
rawBody
();
InputStream
input
=
body
.
byteStream
()
)
{
return
function
.
apply
(
input
);
}
catch
(
Throwable
e
)
{
throw
Exceptions
.
unchecked
(
e
);
...
...
@@ -174,12 +177,12 @@ public class HttpResponse implements ResponseSpec {
@Override
public
MediaType
contentType
()
{
return
body
.
contentType
();
return
this
.
rawBody
()
.
contentType
();
}
@Override
public
long
contentLength
()
{
return
body
.
contentLength
();
return
this
.
rawBody
()
.
contentLength
();
}
@Override
...
...
@@ -190,4 +193,9 @@ public class HttpResponse implements ResponseSpec {
private
static
ResponseBody
ifNullBodyToEmpty
(
@Nullable
ResponseBody
body
)
{
return
body
==
null
?
Util
.
EMPTY_RESPONSE
:
body
;
}
@Override
public
void
close
()
{
Util
.
closeQuietly
(
this
.
rawBody
());
}
}
mica-http/src/main/java/net/dreamlu/mica/http/ResponseSpec.java
浏览文件 @
7dfab7fb
...
...
@@ -343,4 +343,12 @@ public interface ResponseSpec {
consumer
.
accept
(
this
.
rawBody
());
return
this
;
}
/**
* 关闭 ResponseBody
* @since 1.2.0
*/
default
void
close
()
{
// close ResponseBody
}
}
mica-http/src/main/java/net/dreamlu/mica/http/RetryInterceptor.java
浏览文件 @
7dfab7fb
...
...
@@ -50,9 +50,13 @@ public class RetryInterceptor implements Interceptor {
}
// copy 一份 body
ResponseBody
body
=
response
.
peekBody
(
Long
.
MAX_VALUE
);
boolean
tested
=
respPredicate
.
test
(
new
HttpResponse
(
response
));
if
(
tested
)
{
throw
new
IOException
(
"Http Retry ResponsePredicate test Failure."
);
HttpResponse
httpResponse
=
new
HttpResponse
(
response
);
try
{
if
(
respPredicate
.
test
(
httpResponse
))
{
throw
new
IOException
(
"Http Retry ResponsePredicate test Failure."
);
}
}
finally
{
httpResponse
.
close
();
}
return
response
.
newBuilder
().
body
(
body
).
build
();
});
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录