【问题标题】:SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 37312 while http postSocketException:操作系统错误:连接被拒绝,errno = 111,地址 = localhost,端口 = 37312 而 http post
【发布时间】:2021-02-03 05:26:51
【问题描述】:

我正在尝试将数据从颤振发布到节点 API。我的代码如下。

final String uri = "http://localhost:3000";
Map<String, String> userBody = {
"firstName":"Vishal",
"lastName":"Soni",
"email":"vishal@gmail.com",
"mobile":"1234567890",
"password":"123",
};

Future<dynamic> postData() async {
try {
  http.Response res = await http.Client().post("$uri/register", body: jsonEncode(userBody), headers:    {
    'Content-Type': 'application/json',
  });
  print(res);
} catch (e) {
  print(e);
 }
}

出现以下错误

【问题讨论】:

  • 这里没有足够的信息来解决您的问题 - 我将首先验证您的 API 是否正常工作。

标签: flutter dart flutter-web


【解决方案1】:

在终端中运行以下命令

adb reverse tcp:3000 tcp:3000 

【讨论】:

    【解决方案2】:

    您需要更改localhost。它应该是您的 IP 地址。

         final String uri = "http://10.0.2.2:3000";
    

    Github 上查看相同的问题。

    【讨论】:

    • 这仅适用于您在模拟器上运行应用程序的情况,其中 10.0.2.2 循环回主机地址
    • 是的,我在模拟器上运行
    猜你喜欢
    • 2019-12-01
    • 2022-06-18
    • 2022-10-05
    • 1970-01-01
    • 2020-09-03
    • 2023-02-26
    • 1970-01-01
    • 2021-11-05
    • 2018-10-26
    相关资源
    最近更新 更多