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

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

问题描述

我正在使用 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:操作系统错误:连接被拒绝,errno = 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');
    }
}

或非 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

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

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