Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
b7d279d2
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 搜索 >>
提交
b7d279d2
编写于
9月 16, 2021
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
mica-http 添加 HttpException 方便异常时获取相应信息。
上级
b15faab4
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
35 addition
and
13 deletion
+35
-13
mica-http/src/main/java/net/dreamlu/mica/http/AsyncCallback.java
...tp/src/main/java/net/dreamlu/mica/http/AsyncCallback.java
+3
-1
mica-http/src/main/java/net/dreamlu/mica/http/Exchange.java
mica-http/src/main/java/net/dreamlu/mica/http/Exchange.java
+5
-3
mica-http/src/main/java/net/dreamlu/mica/http/HttpException.java
...tp/src/main/java/net/dreamlu/mica/http/HttpException.java
+26
-6
mica-http/src/test/java/net/dreamlu/HttpTest.java
mica-http/src/test/java/net/dreamlu/HttpTest.java
+1
-3
未找到文件。
mica-http/src/main/java/net/dreamlu/mica/http/AsyncCallback.java
浏览文件 @
b7d279d2
...
...
@@ -18,6 +18,7 @@ package net.dreamlu.mica.http;
import
okhttp3.Call
;
import
okhttp3.Callback
;
import
okhttp3.Request
;
import
okhttp3.Response
;
import
javax.annotation.ParametersAreNonnullByDefault
;
...
...
@@ -38,7 +39,8 @@ public class AsyncCallback implements Callback {
@Override
public
void
onFailure
(
Call
call
,
IOException
e
)
{
exchange
.
onFailure
(
call
.
request
(),
new
HttpException
(
e
));
Request
request
=
call
.
request
();
exchange
.
onFailure
(
request
,
new
HttpException
(
request
,
e
));
}
@Override
...
...
mica-http/src/main/java/net/dreamlu/mica/http/Exchange.java
浏览文件 @
b7d279d2
...
...
@@ -65,7 +65,8 @@ public class Exchange {
try
(
HttpResponse
response
=
new
HttpResponse
(
call
.
execute
()))
{
return
func
.
apply
(
response
);
}
catch
(
IOException
e
)
{
failedBiConsumer
.
accept
(
call
.
request
(),
new
HttpException
(
e
));
Request
request
=
call
.
request
();
failedBiConsumer
.
accept
(
request
,
new
HttpException
(
request
,
e
));
return
null
;
}
}
...
...
@@ -79,7 +80,8 @@ public class Exchange {
failedBiConsumer
.
accept
(
call
.
request
(),
new
HttpException
(
response
));
}
}
catch
(
IOException
e
)
{
failedBiConsumer
.
accept
(
call
.
request
(),
new
HttpException
(
e
));
Request
request
=
call
.
request
();
failedBiConsumer
.
accept
(
request
,
new
HttpException
(
request
,
e
));
}
return
null
;
}
...
...
@@ -96,7 +98,7 @@ public class Exchange {
* Returns ok http response.
*
* <p>
*
注意:body 不能读取,因为已经关闭掉了,建议还是直接用 onResponse 函数处理。
* 注意:body 不能读取,因为已经关闭掉了,建议还是直接用 onResponse 函数处理。
* </p>
*
* @return Response
...
...
mica-http/src/main/java/net/dreamlu/mica/http/HttpException.java
浏览文件 @
b7d279d2
...
...
@@ -16,7 +16,11 @@
package
net.dreamlu.mica.http
;
import
javax.annotation.Nullable
;
import
okhttp3.Protocol
;
import
okhttp3.Request
;
import
okhttp3.Response
;
import
javax.annotation.ParametersAreNonnullByDefault
;
import
java.io.IOException
;
/**
...
...
@@ -24,21 +28,20 @@ import java.io.IOException;
*
* @author L.cm
*/
@ParametersAreNonnullByDefault
public
class
HttpException
extends
IOException
{
@Nullable
private
final
ResponseSpec
response
;
public
HttpException
(
ResponseSpec
response
)
{
HttpException
(
ResponseSpec
response
)
{
super
(
response
.
toString
());
this
.
response
=
response
;
}
public
HttpException
(
Throwable
cause
)
{
HttpException
(
Request
request
,
Throwable
cause
)
{
super
(
cause
);
this
.
response
=
null
;
this
.
response
=
getResponse
(
request
,
cause
.
getMessage
())
;
}
@Nullable
public
ResponseSpec
getResponse
()
{
return
response
;
}
...
...
@@ -53,4 +56,21 @@ public class HttpException extends IOException {
}
}
/**
* 构造 HttpResponse
*
* @param request Request
* @param message message
* @return HttpResponse
*/
private
static
HttpResponse
getResponse
(
Request
request
,
String
message
)
{
Response
response
=
new
Response
.
Builder
()
.
request
(
request
)
.
protocol
(
Protocol
.
HTTP_1_1
)
.
message
(
message
)
.
code
(
500
)
.
build
();
return
new
HttpResponse
(
response
);
}
}
mica-http/src/test/java/net/dreamlu/HttpTest.java
浏览文件 @
b7d279d2
...
...
@@ -13,9 +13,7 @@ public class HttpTest {
.
onFailed
((
request
,
e
)
->
{
e
.
printStackTrace
();
ResponseSpec
response
=
e
.
getResponse
();
if
(
response
!=
null
)
{
System
.
out
.
println
(
response
.
asString
());
}
System
.
out
.
println
(
response
.
asString
());
})
.
onSuccessful
(
ResponseSpec:
:
asString
);
System
.
out
.
println
(
html
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录