net_utils.dart 3.4 KB

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