我正在关注Flutter 网络/HTTP 教程向在我的 localhost:8000 上运行的服务器发出 GET 请求。通过浏览器访问我的本地主机工作正常。我的代码如下所示:
var url = 'http://localhost:8000';
Future<String> getUnits(String category) async {
var response = await httpClient.get('$url/$category');
return response.body;
}
当我指向任何真实的 URL 时,这工作正常,例如https://example.com
,但是当我指向https://localhost:8000
or https://localhost
(或这些的任何变体),我收到一个错误,开头为:
E/flutter ( 4879): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 4879): SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 47060
E/flutter ( 4879): #0 IOClient.send (package:http/src/io_client.dart:30:23)
每次重新加载应用程序时,上述错误中的端口都会发生变化。我查看了http包代码而且似乎没有办法指定 URL 的端口。如何指向我的本地主机?
更换字符串localhost
with 10.0.2.2
为我解决了这个问题,因为我在 Android 模拟器中运行代码,而 Android 模拟器在虚拟机中运行。它本质上是重复的这个问题.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)