且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SocketException:操作系统错误:连接被拒绝,errno = 111 in flutter using django backend

更新时间:2022-05-02 08:09:56

Harnish,需要更多细节以调试此错误.

Harnish, need a few more details in-order to debug this error.

  1. 您是在本地运行服务器还是与远程服务器通信?
  2. 您是否在 Android 模拟器上运行该应用?

可能的解决方案:

如果您在本地运行服务器并使用 Android 模拟器,那么您的服务器端点应该是 10.0.2.2:8000 而不是 localhost:8000 作为 AVD 使用10.0.2.2 作为主机环回接口的别名(即)localhost

If you're running the server locally and using the Android emulator, then your server endpoint should be 10.0.2.2:8000 instead of localhost:8000 as AVD uses 10.0.2.2 as an alias to your host loopback interface (i.e) localhost

期货注意事项

我注意到上面的代码正在使用 await 然后在同一行上.这可能会令人困惑,需要明确的是,await 用于暂停执行直到未来完成,而 then 是在未来完成后执行的回调函数.也可以写成下面这样

I noticed above that the code is using await and then on the same line. This can be confusing, to be clear, await is used to suspend execution until a future completes, and then is a callback function to execute after a future completed. The same could be written as below

void myFunction() async {
    var data = {};
    var response = await http.post(URL, headers:headers, body:data);
    if (response.statusCode == 200) {
        print(reponse.body);
    } else {
       print('A network error occurred');
    }
}

或非 async/await 方法

or the non async/await method

void myFunction() {
    var data = {};
    http.post(URL, headers:headers, body:data)
    .then((response) => print(response.body))
    .catchError((error) => print(error));
}

有关 Dart 中期货的更多详细信息,请阅读https://www.dartlang.org/tutorials/language/futures

For a more detailed information on Futures in Dart please read https://www.dartlang.org/tutorials/language/futures