如何解决 Flutter 应用程序中的“连接被拒绝(操作系统错误:连接被拒绝,errno = 111)”?

问题描述 投票:0回答:1

将图像上传到 Node.js 后端时,我的 Flutter 应用程序遇到“连接被拒绝”错误。终端和app错误如下:

终端错误:

> Performing hot restart... Syncing files to device sdk gphone64 x86
> 64... Restarted application in 1,691ms. D/CompatibilityChangeReporter( 9724): Compat change id reported: 78294732; UID 10162; state: DISABLED
> D/MediaScannerConnection( 9724): Scanned
> /data/user/0/com.example.mongo_backend/cache/2fdd677e-2569-4d9c-8349-b461215be87f8625165904540994379.jpg
> to null
> 
> ======== Exception caught by image resource service ================================================ The following SocketException was thrown resolving an image codec: Connection
> refused (OS Error: Connection refused, errno = 111), address =
> localhost, port = 46640
> 
> When the exception was thrown, this was the stack: 
> #0      _NativeSocket.startConnect (dart:io-patch/socket_patch.dart:682:35)
> #1      _RawSocket.startConnect (dart:io-patch/socket_patch.dart:1817:26)
> #2      RawSocket.startConnect (dart:io-patch/socket_patch.dart:27:23)
> #3      Socket._startConnect (dart:io-patch/socket_patch.dart:2038:22)
> #4      Socket.startConnect (dart:io/socket.dart:759:21)
> #5      _ConnectionTarget.connect (dart:_http/http_impl.dart:2453:20)

这是什么意思?

另外,我在 flutter 应用程序上遇到此错误,我尝试使用后端 -node.js- 上传图像,但不起作用:

Flutter 应用程序错误:

我尝试过的:

  • 我已检查我的 Node.js 后端服务器是否正在运行并侦听指定端口(本例中为 46640)。
  • 我已验证没有防火墙规则阻止连接。

具体问题:

如何解决“连接被拒绝”错误并成功将图像从 Flutter 应用上传到 Node.js 后端?

node.js flutter dart image-upload
1个回答
2
投票

使用您的机器 IP 而不是“localhost”来访问您的 API(例如 192.168.x.x)。您的 flutter 应用程序将“localhost”识别为您的模拟器而不是您的 PC(您的 API 在哪里运行),因此有两个不同的本地主机。

© www.soinside.com 2019 - 2024. All rights reserved.