Unable to make requests to my localhost API from Android Emulator

I’m developing a Flutter app using Android Studio and trying to make requests to my API hosted on localhost:44392, which is a .NET Framework 4.6.1 application. When I test the app on a live system, the requests work fine. However, when I change the baseURL to 10.0.2.2, the requests fail, and I get a CONNECTION TIMEOUT error.

Here’s how I’m making the request:

var url = Uri.parse(CONFIG.baseURL + ApiUrl.GetReservationsForCourierEndpoint);
var userId = CONFIG.user_id.toString();
var headerJson = {
  "Content-Type": 'application/json; charset=UTF-8',
  "OperationUserId": userId
};
var filter = jsonEncode(<String, dynamic>{
  'CourierId': CONFIG.user_id,
  'ReservationDate': date.toString()
});

var response = await http.post(url, headers: headerJson, body: filter);
Map<String, dynamic> data = json.decode(utf8.decode(response.bodyBytes));

I’ve tried the following methods but none of them solved the issue:

  1. Adding the android:networkSecurityConfig="@xml/network_security_config" attribute to the android.manifest file and creating a network_security_config.xml file with the following content:
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">10.0.2.2</domain>
    </domain-config>
</network-security-config>

  1. Using adb reverse tcp:44392 tcp:44392 command in PowerShell to forward the port.
  2. Turning off Vulkan by adding the following lines to the .android\advancedFeatures.ini file:
Vulkan = off
GLDirectMem = on

  1. Temporarily disabling Windows Defender and adding an inbound rule to allow port 44392.

The emulator’s connection details are as follows:
IP: 10.0.2.16
** GATEWAY: 10.0.2.2 **
** DNS: 10.0.2.3**

Any help would be appreciated. Thank you!

Is your server set to actually listen on that port? Forwarding the port’s all well and good, but if there’s nothing on the other end to listen to the message, your emulator can shout as much as it likes.

You’ve poked a hole in the wall, but if the other side of the wall is empty air, you’re not going to get much purchase on that screw :wink:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.