Reputation: 149
I have a .net 8 maui app that connects to a webapi (on localhost).
Both apps are running from the same project.
I'm able to connect to the api from the emulator, but cannot connect when running the app from my local device.
I access the api using https://10.0.2.2:"port" - is there something I need to do to give my local device access to make calls to localhost?
Any advice will be great.
Update:
In order to get the working on your local device, first get the ipv4 for your connection, and use this as the base address for your maui application
Secondly, use this same ip address in launchsettings.json "https://ipv4:port" and make sure both are running
Upvotes: 1
Views: 1486
Reputation: 104
To get my .NET MAUI app running on a physical Android device to connect to a local web API, I did the following:
. Allow HTTP/HTTPS Traffic: I ensured my Android app could communicate with my PC by configuring network_security_config.xml to allow traffic to this IP address.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">192.168.1.12</domain> <!-- Your PC's IP -->
<domain includeSubdomains="true">localhost</domain>
</domain-config>
</network-security-config>
Update AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true">
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Purpose of the ApiSettings Class: The ApiSettings class helps manage the API’s base URL based on the platform the app is running on.
public static class ApiSettings
{
public static string ApiUrl = DeviceInfo.Platform == DevicePlatform.Android
? "https://192.168.1.12:7260" // Use your local machine's IP address for Android devices
: "https://localhost:7260"; // Use localhost for other platforms
}
Upvotes: 0
Reputation: 13803
If you are testing your application from a real android device, then you need to put the IP address of your PC while you are trying to connect to your server through APIs. Besides, you also need to make sure they're on the same network.
So, you can recheck the following things:
1.Make sure that the PC (where you are running the server) and the Android device (where you are testing your application) are on the same network (e.g., connected with the same Wifi network).
2.Make sure you are connecting to the IP address of your PC where the server is running. For example, the IP address of your PC is 192.168.1.5. Then, you need to connect to this IP address and call your API as following:
http://192.168.1.5:8000/api/update/68/
You can get the Ip
by ipconfig
command:
3.Make sure you could accept the requests to the port 8000 in your PC. Check your Firewall configuration if it is blocking any incoming requests to the 8000 port. If it is found blocking, then please allow an incoming request to the 8000 port using the following.
sudo ufw allow 8000/tcp
4.Make sure you're using http
only when running server locally.
Upvotes: 1
Reputation: 1
The issue can sometimes be that the API is HTTPS. There are two solutions. The first is to convert the API to HTTP. The second is, as shown in the link below, to run the following command in .NET CLI:
dotnet dev-certs https --trust
After running the command, use the following code:
public static string BaseAddress = DeviceInfo.Platform == DevicePlatform.Android ? "https://10.0.2.2:yourPort" : "https://localhost:yourPort";
public static string yourUrl = $"{BaseAddress}/api/yourApiEndpoint";
Upvotes: 0
Reputation: 1728
Make sure that your webservice is listening on all interfaces ie. 0.0.0.0
Assuming the Android device and the computer are on the same network.
Find out the hostname for your computer (or set up a static ip on your home router for your computer)
point your android app to https://hostname:port/ or https://yourip:port/
Upvotes: 1