2

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)
Kizzle
  • 101
  • 3
  • 17
  • What OS are you on? The URL `http://10.0.2.2:your-port` works for me on windows. (I use Express, so I'm not sure if it makes a difference) – Gabe Aug 12 '20 at 17:25
  • Have you tried the url: `http://127.0.0.1:5001`? – Gabe Aug 12 '20 at 17:27
  • 127 doesn't work as that is the localhost of the device, annoyingly the android emulator has it's own network. The annoying thing is that 10.0.2.2 seems to be the solution that should work but for some reason it won't return a 200. I am on Windows. – Kizzle Aug 12 '20 at 17:43

1 Answers1

1

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.

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.

Kizzle
  • 101
  • 3
  • 17