1

I am developing an app in Android Studio and it makes Http calls to a Node.js web application that I am developing in Visual Studio. All these are on the same Windows 10 PC now.

The Node.js web app is running via Visual Studio 2017 and listening at port 1337. I am able to connect to localhost:1337/ from both Chrome and Edge and verify that the Node.js app is running well. However, from the Android app running in an Android emulator, when trying to connect to localhost:1337/ it got:

Exception: failed to connect to localhost/127.0.0.1 (port 1337): connect failed: ECONNREFUSED (Connection refused)

I disabled the Windows Firewall, but the same problem is still there.

If I deploy the Node.js app to a real web site on AWS, the Android app in the emulator is able to connect to that and work as expected. The downside is that I am unable to step through the Node.js app to debug.

So, on its own, the Node.js app is running fine on my PC and browsers can connect to it. On its own, the Android app is running fine on my PC, and able to connect to the Node.js app on the Internet. The problem is the Android app cannot talk to the Node.js app on the same PC.

Does the problem lie in Visual Studio, Windows or Android Studio?

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • 1
    localhost is not what you think it is for an app that runs in the emulator. There is more than one emulator available for Android, next guess ought to be 10.0.2.2 to connect to the host machine. Then try 169.254.80.80 – Hans Passant Oct 13 '17 at 08:32
  • Thanks. That was the answer. It didn't occur to me to Google for "localhost" instead. – Old Geezer Oct 13 '17 at 09:36
  • @HansPassant, please post a answer for the same, so that the bounty is awarded to the right person – Tarun Lalwani Oct 19 '17 at 08:22

4 Answers4

0

I think , localhost didn't work from android studio, because from emulator android studio you cannot direct access you might be use 10.0.2.2 or 10.0.3.2 , may be this can help you

:)

masadi zainul
  • 397
  • 4
  • 14
0

As said before: localhost (127.0.0.1) is your emulator not your local computer.

check this. How to connect to my http://localhost web server from Android Emulator in Eclipse

so use either the IP address of your computer or 10.0.2.2, which is a special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)

michael - mlc
  • 376
  • 5
  • 5
0

Use your PC IP address. Run ifconfig/ipconfig in terminal/command prompt and look for something like an inet (or whatever looks like an IP address)

Joe Williams
  • 113
  • 1
  • 7
0

Right click on windows icon at extreme bottom left and select windows-shell or cmd and type ipconfig->enter. As it is the same conputer that Node.js is running, use the ipV4 address of ethernet adapter shown, instead of localhost in your app. Hope this helps...