我正在使用 django rest-framework 构建一个 flutter 应用程序。注册 api 在 Postman 中工作正常,但在从 flutter 应用程序成功注册后,它显示上述错误。请求通过 https 地址发送。
删除了 csrf。什么都没发生。
Request:
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
我期望此请求的响应是包含用户和令牌的 Json 对象。
Harnish,需要更多细节才能调试此错误。
- 您是在本地运行服务器还是与远程服务器通信?
- 您是否在 Android 模拟器上运行该应用程序?
可能的解决方案:
如果您在本地运行服务器并使用 Android 模拟器,那么您的服务器端点应该是10.0.2.2:8000
代替localhost:8000
正如 AVD 使用的10.0.2.2
作为主机环回接口的别名(即)localhost
期货注意事项
我注意到上面的代码使用了await,然后在同一行上。这可能会令人困惑,需要明确的是,await
用于暂停执行直到 future 完成,并且then
是 future 完成后执行的回调函数。同样可以写成如下
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 方法
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)