Android Studio 在flutter中接收到完整标头http post之前连接关闭

ffx8fchx  于 2023-02-24  发布在  Android
关注(0)|答案(3)|浏览(287)

我在尝试使用我的API时遇到了问题,在跟进这个问题后,我陷入了困境,我尝试了不同版本的模拟器,问题仍然存在。
错误:

DioError [DioErrorType.other]: HttpException: Connection closed before full header was received, uri = http://10.0.2.2:7108/Users/authenticate

Flutter刮刀

Http帖子

class AuthenticateRemoteApi extends AuthenticateGateway {
  final AuthenticateMapper _authenticateMapper = AuthenticateMapper();

  @override
  Future<SesionUser> login(Authenticate user) async {
    var dio = Dio();
    dio.options.headers['content-Type'] = 'application/json';
    String url = 'http://10.0.2.2:7108/Users/authenticate';

    try {
      Response response = await dio.post(url, data: authenticateModelToJson(user));
      return _authenticateMapper.fromMap(jsonDecode(response.data));
    } catch (e) {
      throw Exception(e);
    }
  }
}
h43kikqp

h43kikqp1#

我通过创建这个拦截器来完成这项工作。
它的想法是当遇到这个随机错误时,只是为了重试请求。

/// Interceptor
class RetryOnConnectionChangeInterceptor extends Interceptor {
  final Dio dio;

  RetryOnConnectionChangeInterceptor({
    required this.dio,
  });

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) async {
  if (_shouldRetryOnHttpException(err)) {
      try {
        handler.resolve(await DioHttpRequestRetrier(dio: dio).requestRetry(err.requestOptions).catchError((e) {
          handler.next(err);
        }));
      } catch (e) {
        handler.next(err);
      }
    } else {
      handler.next(err);
    }

  }

  bool _shouldRetryOnHttpException(DioError err) {
    return err.type == DioErrorType.other &&
        ((err.error is HttpException && err.message.contains('Connection closed before full header was received')));
  }
}

/// Retrier
class DioHttpRequestRetrier {
  final Dio dio;

  DioHttpRequestRetrier({
    required this.dio,
  });

  Future<Response> requestRetry(RequestOptions requestOptions) async {
    return dio.request(
      requestOptions.path,
      cancelToken: requestOptions.cancelToken,
      data: requestOptions.data,
      onReceiveProgress: requestOptions.onReceiveProgress,
      onSendProgress: requestOptions.onSendProgress,
      queryParameters: requestOptions.queryParameters,
      options: Options(
        contentType: requestOptions.contentType,
        headers: requestOptions.headers,
        sendTimeout: requestOptions.sendTimeout,
        receiveTimeout: requestOptions.receiveTimeout,
        extra: requestOptions.extra,
        followRedirects: requestOptions.followRedirects,
        listFormat: requestOptions.listFormat,
        maxRedirects: requestOptions.maxRedirects,
        method: requestOptions.method,
        receiveDataWhenStatusError: requestOptions.receiveDataWhenStatusError,
        requestEncoder: requestOptions.requestEncoder,
        responseDecoder: requestOptions.responseDecoder,
        responseType: requestOptions.responseType,
        validateStatus: requestOptions.validateStatus,
      ),
    );
  }
}

用法:将此拦截器[RetryOnConnectionChangeInterceptor]添加到Dio客户端示例

qq24tv8q

qq24tv8q2#

我认为这可能是一个软件包错误,通常出现在用户没有互联网连接。报告在#1269#377问题从软件包仓库。我建议你使用另一个软件包。

ikfrs5lh

ikfrs5lh3#

在我的情况下,当我在模拟器上构建应用程序时,这个错误总是发生,很少出现在真实的设备上。如果您使用的是Android Studio,请尝试使用Pixel 6 API 33。

相关问题