Explorar el Código

1、添加请求超时设置

tags/0.0.14+3
“yanghuaxuan” hace 3 años
padre
commit
eaf3ae36c7
Se han modificado 1 ficheros con 19 adiciones y 12 borrados
  1. +19
    -12
      lib/util/net_util.dart

+ 19
- 12
lib/util/net_util.dart Ver fichero

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

/// 同步请求
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 ?? {}};

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

Response response;
try {
Dio dio = await NetUtil.getInstance().dio;
Dio dio = await NetUtil
.getInstance()
.dio;
response = await dio.request(
path,
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,
);
} on DioError catch (e) {
@@ -210,12 +213,12 @@ class NetUtil {
/// 异步请求
static void request(String path,
{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 ?? {}};

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

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 解密?
if (isSuccess(result)) {
if (onSuccess != null) {
@@ -239,8 +246,8 @@ class NetUtil {
if (onError != null) {
onError(!EmptyUtil.isEmpty(result)
? !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_MSG])
? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]
: '未知错误'
? result[GlobalConfig.HTTP_RESPONSE_KEY_MSG]
: '未知错误'
: '未知错误');
}
} catch (e) {


Cargando…
Cancelar
Guardar