Android Emulator Connectivity Issues with Expo React Native and Local Express Server

I’m stuck with a problem in my app. I’ve got a React Native frontend using Expo and a local Express backend. Everything’s fine on the web version but the Android emulator won’t connect to the backend. Here’s what I’ve tried:

  1. Changed the API URL in the config file to:

    • http://10.0.2.2:3000
    • My local IP address
    • http://localhost:3000
  2. Updated app.json to allow cleartext traffic:

"expo": {
  "android": {
    "usesCleartextTraffic": true
  }
}
  1. Made sure the backend is running (works in browser)
  2. Tested API URL in Postman with my local IP (works)

I’ve also set up CORS to allow all origins. Here’s a snippet of my API call:

async function loginUser(email, password) {
  const result = await fetch(`${API_URL}/user/signin`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ email, password })
  });
  const data = await result.json();
  if (!result.ok) throw new Error(data.message);
  return data;
}

Any ideas what might be causing this? I’m out of options here.

hey, have u tried using ngrok? it creates a secure tunnel to your localhost. install and run it with your port; then update the url in ur app. might solve ur issue without code changes!

have you checked if theres any firewall or antivirus blocking the connection? sometimese they can be sneaky! also, whats ur android emulator version? older ones might have quirks with networking. oh, and have u tried running the app on a physical device? could help narrow down if its an emulator-specific issue :thinking:

I’ve encountered similar issues with Android emulators and local servers. One often overlooked solution is to use the special IP 10.0.2.2, which the Android emulator uses to communicate with the host machine’s localhost.

Have you tried explicitly allowing network access in your Android manifest? Add this permission to your app’s AndroidManifest.xml:

Also, ensure your Express server is configured to listen on all network interfaces, not just localhost. In your server code, try:

app.listen(3000, ‘0.0.0.0’, () => {
console.log(‘Server running on all network interfaces’);
});

Lastly, double-check your firewall settings. Sometimes, they can block connections from the emulator. Temporarily disabling the firewall for testing might help isolate the issue.