Browse Source

1、添加请求超时设置

tags/0.0.14+3
“yanghuaxuan” 3 years ago
parent
commit
eaf3ae36c7
1 changed files with 19 additions and 12 deletions
  1. +19
    -12
      lib/util/net_util.dart

+ 19
- 12
lib/util/net_util.dart View File

@@ -96,7 +96,8 @@ class NetUtil {


/// 同步请求 /// 同步请求
static Future<dynamic> post(String path, static Future<dynamic> post(String path,
{Map<String, dynamic> params, Map<String, dynamic> queryParameters, NetMethod method = NetMethod.POST, bool cache = false, bool showToast = true}) async {
{Map<String, dynamic> params, Map<String, dynamic> queryParameters, NetMethod method = NetMethod
.POST, bool cache = false, bool showToast = true, int timeOut = 15000}) async {
var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}};


// 根据请求参数,获取缓存的Key // 根据请求参数,获取缓存的Key
@@ -109,11 +110,13 @@ class NetUtil {


Response response; Response response;
try { try {
Dio dio = await NetUtil.getInstance().dio;
Dio dio = await NetUtil
.getInstance()
.dio;
response = await dio.request( response = await dio.request(
path, path,
data: !EmptyUtil.isEmpty(bodyParams) ? bodyParams : null, data: !EmptyUtil.isEmpty(bodyParams) ? bodyParams : null,
options: Options(method: enumToString(method), headers: headParam),
options: Options(method: enumToString(method), headers: headParam, receiveTimeout: timeOut),
queryParameters: !EmptyUtil.isEmpty(queryParameters) ? queryParameters : null, queryParameters: !EmptyUtil.isEmpty(queryParameters) ? queryParameters : null,
); );
} on DioError catch (e) { } on DioError catch (e) {
@@ -210,12 +213,12 @@ class NetUtil {
/// 异步请求 /// 异步请求
static void request(String path, static void request(String path,
{NetMethod method = NetMethod.GET, {NetMethod method = NetMethod.GET,
Map<String, dynamic> params,
Map<String, dynamic> queryParameters,
OnSuccess onSuccess,
OnError onError,
OnCache onCache,
bool showToast = true}) async {
Map<String, dynamic> params,
Map<String, dynamic> queryParameters,
OnSuccess onSuccess,
OnError onError,
OnCache onCache,
bool showToast = true,int timeOut=15000}) async {
var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}}; var paramsData = {'postData': params ?? {}, 'queryParameters': queryParameters ?? {}};


// 根据请求参数,获取缓存的Key // 根据请求参数,获取缓存的Key
@@ -227,7 +230,11 @@ class NetUtil {
} }


try { try {
Map result = await NetUtil.post(path, method: method, params: params, queryParameters: queryParameters, showToast: showToast, cache: onCache != null);
Map result = await NetUtil.post(path, method: method,
params: params,
queryParameters: queryParameters,
showToast: showToast,
cache: onCache != null,timeOut: timeOut);
// TODO 解密? // TODO 解密?
if (isSuccess(result)) { if (isSuccess(result)) {
if (onSuccess != null) { if (onSuccess != null) {
@@ -239,8 +246,8 @@ class NetUtil {
if (onError != null) { if (onError != null) {
onError(!EmptyUtil.isEmpty(result) onError(!EmptyUtil.isEmpty(result)
? !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]) ? !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG])
? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]
: '未知错误'
? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]
: '未知错误'
: '未知错误'); : '未知错误');
} }
} catch (e) { } catch (e) {


Loading…
Cancel
Save