在Android模拟器中无法访问localhost:portnumber [英] Trouble accessing localhost:portnumber in android emulator

查看:106
本文介绍了在Android模拟器中无法访问localhost:portnumber的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于我的问题,我在github上以及这里都看到了很多帖子,但是它仍然存在.我有一个托管在端口5001上的ASP.NET API,该API返回JSON对象.我尝试将URL更改为指向10.0.2.2:5001代替localhost,在端口10.0.2.2中添加端口号作为模拟器上的代理,将emualtor降级为Pie.这些都不对我有用.这是有问题的代码部分:

I have seen numerous posts on the flutter github as well as here regarding my issue however it still persists. I have an ASP.NET API that is being hosted on port 5001 that returns a JSON object. I have tried changing the URL to point to 10.0.2.2:5001 in place of localhost, adding 10.0.2.2 with the port number as a proxy on the emulator, downgrading the emualtor to Pie. None of these have worked for me. Here is the troublesome section of code in question:

  Future<Foo> fetchJson() async {
    var response = await client.get(
        "http://10.0.2.2:5001/api/SampleApp/FetchObject",
        headers: {"Accept": "application/json", "Content": "application/json"});
    var something = Foo.fromJson(jsonDecode(response.body));
    return something;
  }

Exception has occurred.
ClientException (Connection closed before full header was received)

推荐答案

在花费数小时寻找该问题的解决方案之后,事实证明,使用ASP.NET API时启用SSL是一个问题.为了解决此问题,我在后端解决方案的项目属性中禁用了启用SSL的选项,并使用 ngrok 来创建本地主机公开可访问的端口.他们也有一个Visual Studio扩展程序,这使得打开隧道真的很容易.

After wasting hours on finding a solution to this problem it turns out it is an issue with SSL being enable when using an ASP.NET API. To resolve this issue I disabled the SSL enabled option in the project properties in my backend solution and used ngrok to make the localhost port publicly acccessible. They have a visual studio extension too which makes it really easy to open a tunnel.

请注意,此问题仅与针对本地主机运行的API有关,部署后,您的仿真器应该能够从已部署的URL ping API,而不会出现问题.我认为这与模拟器或物理android设备无法使用启用了SSL的10.0.2.2/10.0.3.2(Genymotion)有关,因为该请求将由于证书而被拒绝.

Note that this issue is only relevant to your API being run against localhost, when it is deployed then your emulator should be able to ping the API from the deployed URL without issue. I believe it is to do with an emulator or physical android device not being able to use 10.0.2.2/10.0.3.2(Genymotion) with SSL enabled as the request will be rejected due to the certificate.

这篇关于在Android模拟器中无法访问localhost:portnumber的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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