If none of the solutions above worked for you (as they did not for me), probably you have SSL issues, but not on the server side
During development our SSL certs are self-signed, those certs are invalid in the point of view of an Android (so they are for emulators to)
When creating the instance of your httpclient for the MAUI app, do this:
public class APIService : IAPIService
{
HttpClient _httpClient;
JsonSerializerOptions _jsonSerializerOptions;
string _baseURI;
HttpClientHandler handler = new HttpClientHandler();
public APIService()
{
#if DEBUG
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) =>
{
if (cert != null && cert.Issuer.Equals("CN=localhost"))
{
return true;
}
return errors == System.Net.Security.SslPolicyErrors.None;
};
_httpClient = new HttpClient(handler);
#else
_httpClient = new HttpClient();
#endif
_jsonSerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};
_baseURI = DeviceInfo.Platform == DevicePlatform.Android ? "https://10.0.2.2:5001" : "https://localhost:5001";
}
}
on your launchSettings.json API file, add a local profile:
"local": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5000;https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
I recommend 0.0.0.0:5000 & 0.0.0.0:5001 because your API will listen to all address, useful when running multiple applications
worked for me.
But, just in case you have troubles, add a subdomain. By default 10.0.2.2 should work but I saw people complaining.
On your Platforms/Android folder, create a folder named xml, then create a .xml file called network_security_config.xml and add this:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">10.0.2.2</domain>
</domain-config>
</network-security-config>
Go to your Androidmanifest file, right-click him and select "Open with" => xml editor, then add this to your manifest tag:
android:networkSecurityConfig="@xml/network_security_config"
with all that set, things should be fine.
Hope it helps
You can see the fully explained tutorial here