fix(mobile/telemetry): add NativeAdapter to heartbeat & uploader, fix 2xx status check
- HeartbeatService: add NativeAdapter on Android to fix DNS via Java stack; fix status check from == 200 to >= 200 && < 300 - TelemetryUploader: same two fixes (refactor to _buildDio static method) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
9e368cbf37
commit
13ab4ba1f3
|
|
@ -1,6 +1,8 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:native_dio_adapter/native_dio_adapter.dart';
|
||||
import '../session/session_manager.dart';
|
||||
import '../session/session_events.dart';
|
||||
import 'presence_config.dart';
|
||||
|
|
@ -81,6 +83,9 @@ class HeartbeatService {
|
|||
connectTimeout: const Duration(seconds: 5),
|
||||
receiveTimeout: const Duration(seconds: 5),
|
||||
));
|
||||
if (Platform.isAndroid) {
|
||||
_dio.httpClientAdapter = NativeAdapter();
|
||||
}
|
||||
|
||||
// 监听会话状态变化
|
||||
final sessionManager = SessionManager();
|
||||
|
|
@ -184,7 +189,8 @@ class HeartbeatService {
|
|||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final code = response.statusCode ?? 0;
|
||||
if (code >= 200 && code < 300) {
|
||||
_lastHeartbeatAt = DateTime.now();
|
||||
_heartbeatCount++;
|
||||
debugPrint('💓 [Heartbeat] Sent #$_heartbeatCount');
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:native_dio_adapter/native_dio_adapter.dart';
|
||||
import '../storage/telemetry_storage.dart';
|
||||
|
||||
/// 遥测上传器
|
||||
|
|
@ -20,11 +22,20 @@ class TelemetryUploader {
|
|||
required this.apiBaseUrl,
|
||||
required this.storage,
|
||||
this.getAuthHeaders,
|
||||
}) : _dio = Dio(BaseOptions(
|
||||
baseUrl: apiBaseUrl,
|
||||
}) : _dio = _buildDio(apiBaseUrl);
|
||||
|
||||
static Dio _buildDio(String baseUrl) {
|
||||
final dio = Dio(BaseOptions(
|
||||
baseUrl: baseUrl,
|
||||
connectTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 10),
|
||||
));
|
||||
// 与 ApiClient 保持一致:Android 上走 OkHttp,解决 Dart native socket DNS 问题
|
||||
if (Platform.isAndroid) {
|
||||
dio.httpClientAdapter = NativeAdapter();
|
||||
}
|
||||
return dio;
|
||||
}
|
||||
|
||||
/// 启动定时上传(每30秒或累积20条)
|
||||
void startPeriodicUpload({
|
||||
|
|
@ -75,13 +86,14 @@ class TelemetryUploader {
|
|||
),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
// 上传成功,删除本地记录
|
||||
final code = response.statusCode ?? 0;
|
||||
if (code >= 200 && code < 300) {
|
||||
// 上传成功(后端 POST 返回 201 Created)
|
||||
await storage.removeEvents(events.length);
|
||||
debugPrint('✅ Uploaded ${events.length} telemetry events');
|
||||
return true;
|
||||
} else {
|
||||
debugPrint('❌ Upload failed: ${response.statusCode}');
|
||||
debugPrint('❌ Upload failed: $code');
|
||||
return false;
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue