Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
N
netrain_flutter_app
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
邹志胜
netrain_flutter_app
Commits
bb8eff15
Commit
bb8eff15
authored
Jul 21, 2021
by
“Icebear”
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加请求拦截器
parent
fcd57b20
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
2 deletions
+29
-2
HttpManager.dart
netrain_flutter_app/lib/zhangfeng/Network/HttpManager.dart
+29
-2
No files found.
netrain_flutter_app/lib/zhangfeng/Network/HttpManager.dart
View file @
bb8eff15
...
...
@@ -16,8 +16,35 @@ class HttpManager {
///通用全局单例,第一次使用时初始化
HttpManager
.
_internal
({
String
baseUrl
})
{
if
(
null
==
_dio
)
{
///connectTimeout连接超时时长
///receiveTimeout响应流上前后两次接受到数据的间隔,单位为毫秒。如果两次间隔超过[receiveTimeout],[Dio] 将会抛出一个[DioErrorType.RECEIVE_TIMEOUT]的异常.注意: 这并不是接收数据的总时限
///sendTimeout请求超时时长
///如果返回数据是json(content-type),dio默认会自动将数据转为json,
///
_dio
=
new
Dio
(
new
BaseOptions
(
baseUrl:
UrlPath
.
baseUrl
,
connectTimeout:
15000
));
baseUrl:
UrlPath
.
baseUrl
,
connectTimeout:
15000
,
sendTimeout:
15000
));
// 添加拦截器
_dio
.
interceptors
.
add
(
InterceptorsWrapper
(
onRequest:
(
RequestOptions
options
,
RequestInterceptorHandler
hander
)
{
print
(
"
\n
================== 请求数据 =========================="
);
print
(
"url =
${options.uri.toString()}
"
);
print
(
"headers =
${options.headers}
"
);
print
(
"params =
${options.data}
"
);
hander
.
next
(
options
);
},
onResponse:
(
Response
response
,
ResponseInterceptorHandler
handler
)
{
print
(
"
\n
================== 响应数据 =========================="
);
print
(
"code =
${response.statusCode}
"
);
print
(
"data =
${response.data}
"
);
print
(
"
\n
"
);
handler
.
next
(
response
);
},
onError:
(
DioError
e
,
ErrorInterceptorHandler
handler
)
{
print
(
"
\n
================== 错误响应数据 ======================"
);
print
(
"type =
${e.type}
"
);
print
(
"message =
${e.message}
"
);
print
(
"stackTrace =
${e.stackTrace}
"
);
print
(
"
\n
"
);
handler
.
next
(
e
);
}));
}
}
...
...
@@ -73,7 +100,7 @@ class HttpManager {
map
[
UserModel
.
getModel
().
header
]
=
UserModel
.
getModel
().
tokenHead
+
UserModel
.
getModel
().
token
;
}
Response
response
=
await
_dio
.
request
(
url
,
queryParameters:
parameters
,
data:
formdata
,
options:
new
Options
(
method:
method
,
headers:
map
));
queryParameters:
parameters
,
data:
formdata
,
options:
new
Options
(
method:
method
,
headers:
map
,
responseType:
ResponseType
.
plain
));
Map
<
String
,
dynamic
>
result
=
json
.
decode
(
response
.
toString
());
BaseHttpModel
httpModel
=
BaseHttpModel
.
fromJson
(
result
);
if
(
httpModel
.
isSuccess
())
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment