net_utils.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'package:common_utils/common_utils.dart';
  2. import 'package:dio/dio.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:liftmanager/utils/url.dart';
  5. /*
  6. * Http请求配置工具类
  7. */
  8. class NetUtils {
  9. static BuildContext context = null;
  10. BaseOptions _options;
  11. Dio dio;
  12. // 工厂模式
  13. factory NetUtils() => _getInstance();
  14. static NetUtils get instance => _getInstance();
  15. static NetUtils _instance;
  16. NetUtils._internal() {
  17. //初始化
  18. dio = getDio();
  19. }
  20. static NetUtils _getInstance() {
  21. LogUtil.init(isDebug: false,tag: "****NetUtils****");
  22. if (_instance == null) {
  23. _instance = new NetUtils._internal();
  24. }
  25. return _instance;
  26. }
  27. /**
  28. * 获取dio实例,不配置根url,完全使用传入的绝对路径url
  29. */
  30. Dio getDio({String url, BaseOptions options}) {
  31. if (options == null) {
  32. _options = new BaseOptions(
  33. baseUrl: baseUrl,
  34. connectTimeout: 1500000,
  35. receiveTimeout: 15000,
  36. contentType: "multipart/form-data",
  37. );
  38. } else {
  39. _options = options;
  40. }
  41. Dio _dio = new Dio(_options);
  42. // _dio.interceptors.add(new TokenInterceptor());//待完善
  43. // _dio.interceptors.add(new ErrorInterceptor(_dio));//待优化
  44. // _dio.interceptors.add(new HeaderInterceptor());
  45. // _dio.interceptors.add(new LogInterceptor());
  46. // setProxy(_dio);
  47. return _dio;
  48. }
  49. /**
  50. * 设置代理
  51. * */
  52. // void setProxy(Dio dio) {
  53. // //debug模式且为wifi网络时设置代理
  54. // if (Config.debug) {
  55. // //debug模式下设置代理
  56. // (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
  57. // (client) {
  58. // //设置代理
  59. // client.findProxy = (uri) {
  60. // return "PROXY " + UrlConstant.PROXY_URI;
  61. // };
  62. // };
  63. // }
  64. // }
  65. Future<Response> post(BuildContext context, url, {data, BaseOptions options,cancelToken}) async {
  66. LogUtil.v('启动post请求 url:$url ,body: $data');
  67. Response response;
  68. try {
  69. if (url != null &&
  70. (url.startsWith("http://") || url.startsWith("https://"))) {
  71. dio = getDio(url: url,options: options);
  72. }
  73. response = await dio.post(url, data: data, cancelToken: cancelToken);
  74. print('post请求成功 response.data:${response.statusCode.toString()}');
  75. return response;
  76. // LogUtil.v('post请求成功 response.data:${response.toString()}');
  77. } on DioError catch (e) {
  78. if (CancelToken.isCancel(e)) {
  79. LogUtil.v('post请求取消:' + e.message);
  80. }
  81. LogUtil.v('post请求发生错误:$e');
  82. }
  83. return response; //response.data.toString()这种方式不是标准json,不能使用
  84. }
  85. get(BuildContext context, url, {data,BaseOptions options,cancelToken}) async {
  86. LogUtil.v('启动get请求 url:$url ,body: $data');
  87. Response response;
  88. try {
  89. if (url != null &&
  90. (url.startsWith("http://") || url.startsWith("https://"))) {
  91. dio = getDio(url: url,options: options);
  92. }
  93. response =
  94. await dio.get(url, queryParameters: data, cancelToken: cancelToken);
  95. LogUtil.v('get请求成功 response.data:${response.toString()}');
  96. } on DioError catch (e) {
  97. if (CancelToken.isCancel(e)) {
  98. LogUtil.v('get请求取消:' + e.message);
  99. }
  100. LogUtil.v('get请求发生错误:$e');
  101. }
  102. return response;
  103. }
  104. }