Search code examples
c#androidhttpandroid-permissionsmaui

Why does the Android version of my MAUI app not have internet access?


In a .NET MAUI project, I have this line:

var result = await client.GetStringAsync(https://localhost:7239/products/);

When compiled to a Windows app, it works as intended; when compiled to an Android app, I get this error: "Failed to connect to localhost/127.0.0.1:7239".

Things I have already thought about checking:

  • The emulated Android phone does have internet access. I tested its YouTube app.
  • The AndoidManifest.xml file already contains <uses-permission android:name="android.permission.INTERNET" />.
  • Microsoft.Maui.ApplicationModel.Permissions does not have an internet access member I can request at runtime.

I would appreciate advice. I should mention that I am quite new to C#, and a complete noob with respect to MAUI and phone app development.


Solution

  • Per the Android developer docs you can use 10.0.2.2 when inside the emulator to reference the host machine.