【发布时间】:2019-04-19 22:36:37
【问题描述】:
在我的颤振应用程序中,我有用户注册表单,它使用 http 请求将数据发送到服务器。
每当我提交表单并使用 emulator(API 27 和 android 版本 8.1)发送请求时,它都可以正常工作,但在 real android 设备 中会出现错误本地主机服务器
SocketException: OS Error: Connection refused, errno = 111, address = 127.0.0.1, port = 48134
我正在使用版本为 7.1 的 Android 设备
代码:
final JsonDecoder _decoder = new JsonDecoder();
Future<dynamic> post() {
return http.post(
"http://192.168.31.16:9091/myprj/api/register",
body: json.encode({
"username": "rahul",
"password": "rahul",
"email": "myemail@gmail.com",
}),
headers: {"Accept": "application/json","Content-type": "application/json",},
).then((http.Response response) {
final String res = response.body;
final int statusCode = response.statusCode;
if (statusCode < 200 || statusCode > 400 || json == null) {
throw new Exception("Error while fetching data");
}
return _decoder.convert(res);
});
}
在哪里,我使用了本地网络 IP 192.168.31.16 而不是 localhost。
当我使用实时服务器地址时,它也可以使用 android 设备正常工作。问题仅在于具有真正的 android 设备的 localhost http 请求服务器。
希望你能理解我的问题。
【问题讨论】:
-
您需要在Android中添加互联网权限。默认情况下,它仅在调试版本中。
-
对不起,这似乎只是计划中的,但尚未实施github.com/flutter/flutter/issues/20789
-
那么你在设备上运行的时候需要连接本地主机吗?
-
@SamiKanafani 无法使用 android 设备调用在 localhost 上运行的 REST API
标签: dart flutter localhost httprequest