且构网

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

SocketException:操作系统错误:使用django后端,连接被拒绝,errno = 111抖动

更新时间:2021-09-08 08:32:18

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');
    }
}

或非异步/等待方法

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