refactor(auto_track): 替换Dio为HttpClient并调整异常处理
移除了对Dio的依赖,改为使用HttpClient进行网络请求,并更新了异常处理逻辑以适应新的HttpException。同时优化了日志记录的相关配置。
Showing
3 changed files
with
26 additions
and
21 deletions
1 | import 'dart:async'; | 1 | import 'dart:async'; |
2 | +import 'dart:convert'; | ||
3 | +import 'dart:io'; | ||
2 | import 'dart:math'; | 4 | import 'dart:math'; |
3 | 5 | ||
4 | import 'package:auto_track/auto_track/config/manager.dart'; | 6 | import 'package:auto_track/auto_track/config/manager.dart'; |
5 | import 'package:auto_track/auto_track/utils/track_model.dart'; | 7 | import 'package:auto_track/auto_track/utils/track_model.dart'; |
6 | -import 'package:dio/dio.dart'; | ||
7 | 8 | ||
8 | import '../log/logger.dart'; | 9 | import '../log/logger.dart'; |
9 | 10 | ||
10 | 11 | ||
11 | - | ||
12 | class AutoTrackQueue { | 12 | class AutoTrackQueue { |
13 | static final AutoTrackQueue instance = AutoTrackQueue._(); | 13 | static final AutoTrackQueue instance = AutoTrackQueue._(); |
14 | AutoTrackQueue._(); | 14 | AutoTrackQueue._(); |
15 | 15 | ||
16 | Timer? _timer; | 16 | Timer? _timer; |
17 | final List<TrackModel> _queue = []; | 17 | final List<TrackModel> _queue = []; |
18 | - final dio = Dio(); | 18 | + final httpClient = HttpClient(); |
19 | 19 | ||
20 | void appendQueue(TrackModel model) { | 20 | void appendQueue(TrackModel model) { |
21 | if (_timer == null) return; | 21 | if (_timer == null) return; |
@@ -48,20 +48,25 @@ class AutoTrackQueue { | @@ -48,20 +48,25 @@ class AutoTrackQueue { | ||
48 | } | 48 | } |
49 | if (host != null) { | 49 | if (host != null) { |
50 | final t = DateTime.now().millisecondsSinceEpoch; | 50 | final t = DateTime.now().millisecondsSinceEpoch; |
51 | - dio.post(host, data: { | ||
52 | - 'app_key': config.appKey ?? '', | ||
53 | - 'signature': config.signature!(t), | ||
54 | - 't': t, | ||
55 | - 'user_id': config.userId ?? '', | ||
56 | - 'track_id': config.trackId ?? '', | ||
57 | - 'unique_id': config.uniqueId ?? AutoTrackConfigManager.instance.deviceId, | ||
58 | - 'device_id': AutoTrackConfigManager.instance.deviceId, | ||
59 | - 'data_list': uploadList.map((e) => e.toMap()).toList(), | ||
60 | - 'app_version': AutoTrackConfigManager.instance.appVersion, | ||
61 | - 'device_info': AutoTrackConfigManager.instance.deviceInfo | ||
62 | - }).onError((error, stackTrace) { | ||
63 | - AutoTrackLogger.getInstance().error(error!); | ||
64 | - return Future.value(Response(statusCode: 500, requestOptions: RequestOptions(path: host))); | 51 | + httpClient.postUrl(Uri.parse(host)).then((request) { |
52 | + request.headers.set(HttpHeaders.contentTypeHeader, 'application/json'); | ||
53 | + request.write(json.encode({ | ||
54 | + 'app_key': config.appKey ?? '', | ||
55 | + 'signature': config.signature!(t), | ||
56 | + 't': t, | ||
57 | + 'user_id': config.userId ?? '', | ||
58 | + 'track_id': config.trackId ?? '', | ||
59 | + 'unique_id': config.uniqueId ?? AutoTrackConfigManager.instance.deviceId, | ||
60 | + 'device_id': AutoTrackConfigManager.instance.deviceId, | ||
61 | + 'data_list': uploadList.map((e) => e.toMap()).toList(), | ||
62 | + 'app_version': AutoTrackConfigManager.instance.appVersion, | ||
63 | + 'device_info': AutoTrackConfigManager.instance.deviceInfo | ||
64 | + })); | ||
65 | + return request.close(); | ||
66 | + }).then((response) { | ||
67 | + AutoTrackLogger.getInstance().debug('upload status => ${response.statusCode}'); | ||
68 | + }).catchError((error) { | ||
69 | + AutoTrackLogger.getInstance().error(error); | ||
65 | }); | 70 | }); |
66 | } | 71 | } |
67 | } | 72 | } |
1 | -import 'package:dio/dio.dart'; | 1 | +import 'dart:io'; |
2 | + | ||
2 | import 'package:flutter/widgets.dart'; | 3 | import 'package:flutter/widgets.dart'; |
3 | 4 | ||
4 | typedef AutoTrackLoggerHandler = void Function(AutoTrackLoggerLevel level, String message); | 5 | typedef AutoTrackLoggerHandler = void Function(AutoTrackLoggerLevel level, String message); |
@@ -26,8 +27,8 @@ class AutoTrackLogger { | @@ -26,8 +27,8 @@ class AutoTrackLogger { | ||
26 | message = e.message; | 27 | message = e.message; |
27 | } else if (e is Error) { | 28 | } else if (e is Error) { |
28 | message = e.stackTrace.toString(); | 29 | message = e.stackTrace.toString(); |
29 | - } else if (e is DioException) { | ||
30 | - message = (e).message ?? 'dio exception with unknown message'; | 30 | + } else if (e is HttpException) { |
31 | + message = e.message; | ||
31 | } | 32 | } |
32 | _print(AutoTrackLoggerLevel.error, message); | 33 | _print(AutoTrackLoggerLevel.error, message); |
33 | } | 34 | } |
@@ -10,7 +10,6 @@ environment: | @@ -10,7 +10,6 @@ environment: | ||
10 | dependencies: | 10 | dependencies: |
11 | crypto: ^3.0.3 | 11 | crypto: ^3.0.3 |
12 | device_info_plus: ^9.1.2 | 12 | device_info_plus: ^9.1.2 |
13 | - dio: ^5.4.1 | ||
14 | flutter: | 13 | flutter: |
15 | sdk: flutter | 14 | sdk: flutter |
16 | package_info_plus: ^8.0.1 | 15 | package_info_plus: ^8.0.1 |
-
Please register or login to post a comment