| | |
| | | import '../ui/widget/dialog.dart'; |
| | | import '../utils/ui_utils.dart'; |
| | | import '../model/common/http_model.dart'; |
| | | import 'package:dio/dio.dart'; |
| | | import 'package:dio/adapter.dart'; |
| | | |
| | | typedef OnHttpRequestFinish = void Function(HttpRequestResult result); |
| | | |
| | |
| | | // params ??= {}; |
| | | params = await getBaseParams(params); |
| | | |
| | | var httpClient = HttpClient(); |
| | | httpClient.connectionTimeout = const Duration(seconds: 20); |
| | | var uri = Uri( |
| | | scheme: "http", |
| | | host: "api.location.izzql.com", |
| | | path: api, |
| | | port: 8090, |
| | | queryParameters: params); |
| | | |
| | | // var uri = Uri( |
| | | // scheme: "http", |
| | | // host: "192.168.3.122", |
| | | // path: api, |
| | | // port: 8082, |
| | | // queryParameters: params); |
| | | |
| | | print("uri:$uri"); |
| | | |
| | | if (onStart != null) { |
| | | onStart(); |
| | | } |
| | | HttpRequestResult requestResult; |
| | | |
| | | try { |
| | | HttpClientRequest request = await httpClient.postUrl(uri); |
| | | var response = await request.close(); |
| | | var dio = Dio() |
| | | ..options = BaseOptions( |
| | | baseUrl: "http://api.hanju.goxcw.com:8089/BuWan", |
| | | connectTimeout: 20000, |
| | | receiveTimeout: 1000 * 60, |
| | | contentType: "application/x-www-form-urlencoded"); |
| | | //设置代理 |
| | | (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = |
| | | (HttpClient client) { |
| | | client.findProxy = (uri) { |
| | | return 'PROXY 192.168.3.122:8888'; |
| | | }; |
| | | client.badCertificateCallback = |
| | | (X509Certificate cert, String host, int port) => true; |
| | | }; |
| | | |
| | | // FormData formData = FormData.fromMap(params); |
| | | var response = await dio.post( |
| | | api, |
| | | data: params, |
| | | onSendProgress: (int sent, int total) { |
| | | print('$sent $total'); |
| | | }, |
| | | ); |
| | | if (response.statusCode == HttpStatus.ok) { |
| | | String result = await response.transform(const Utf8Decoder()).join(); |
| | | String result = response.data.toString(); |
| | | print("网络请求结果:$result"); |
| | | requestResult = HttpRequestResult(true, jsonDecode(result)); |
| | | } else { |
| | | requestResult = HttpRequestResult(false, null, msg: "网络请求失败"); |
| | | } |
| | | } on TimeoutException catch (_) { |
| | | requestResult = HttpRequestResult(false, null, msg: "网络请求超时"); |
| | | } on SocketException catch (_) { |
| | | Fluttertoast.showToast(msg: "网络请求出错"); |
| | | } on DioError catch (_) { |
| | | if (_.type == DioErrorType.connectTimeout || |
| | | _.type == DioErrorType.receiveTimeout || |
| | | _.type == DioErrorType.sendTimeout) { |
| | | requestResult = HttpRequestResult(false, null, msg: "网络请求超时"); |
| | | } else { |
| | | requestResult = HttpRequestResult(false, null, msg: "网络请求出错"); |
| | | } |
| | | } catch (e) { |
| | | requestResult = HttpRequestResult(false, null, msg: "网络请求出错"); |
| | | } |
| | | if (notifyError && !requestResult.success) { |