SocketException:操作系统错误:使用django后端,连接被拒绝,errno = 111抖动 [英] SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend

查看:297
本文介绍了SocketException:操作系统错误:使用django后端,连接被拒绝,errno = 111抖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django rest-framework开发一个Flutter应用程序。注册api在Postman中运行正常,但是从flutter应用程序成功注册后,它显示上述错误。该请求已通过https地址发送。

I m building a flutter app with django rest-framework. The registration api is working fine in Postman but after some successful registration from the flutter app it is showing the above error. The request is been sent on https address.

已删除csrf。什么都没发生。

Removed csrf. Nothing happens.

var data = {'email':signupemailidcontroller.text,
            'password1':passwordcontroller.text,
            'password2':confirmpasswordcontroller.text,
           };
        //http request here
        await http.post(websitesignupurl,
                        headers: headers,
                        body: json.encode(data))
          .then((onResponse){
            print(onResponse.body);
          }).catchError((onerror){
            print(onerror.toString());
        });



控制台中的输出:



SocketException:操作系统错误:连接被拒绝,错误号= 111

Output in Console:

SocketException: OS Error: Connection refused, errno = 111

我希望此请求的响应是一个包含用户和令牌的Json对象。

I Expect the response of this request to be a Json object containing the user and token.

推荐答案

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

这篇关于SocketException:操作系统错误:使用django后端,连接被拒绝,errno = 111抖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆