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

Time:12-14

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.

CodePudding user response:

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

CodePudding user response:

You can handle your problem according to your specific situation.

First, the localhost refers to the device on which the code is running. As far as your question is concerned, it is the android emulator. In general, we need to use the actual IP or the FQDN of the web url that we want to access,just as Jason mentioned.

Second,if you want to refer to the computer which is running the Android simulator, use the IP address 10.0.2.2.(Remember this solution only works on emulators)

For more about this, you can check document Set up Android Emulator networking.

Note:

You can get the IP by command line:

  ipconfig /all
  • Related