3

I have a node.js server running on my local system which interacts with postgresql in the same system to fetch and save data. I want to access node.js server from my android app to save and fetch data from postresql. The problem is my app is not able to connect to localhost. As localhost for my phone is different from my local system and if I am providing my system's IP address then also its refusing the connection.

Arnold Schrijver
  • 3,588
  • 3
  • 36
  • 65
vishal gaurav
  • 686
  • 1
  • 9
  • 24
  • what is your question? – Alan Francis Jan 13 '16 at 09:22
  • You would simply access it using your local address, which is 127.0.0.1. Don't forget to specify http and the port number as well, as per usual. – Ewald Jan 13 '16 at 09:28
  • 1
    127.0.0.1 is for localhost which would not be the correct IP address if I am accessing it from android app in my phone. – vishal gaurav Jan 13 '16 at 09:34
  • Maybe you can replace `127.0.0.1` to `*`. because `*` stands for all ip address. – BlackMamba Jan 13 '16 at 09:37
  • @BlackMamba That will not work. – vishal gaurav Jan 13 '16 at 10:37
  • Make the `Node.js` server listen on your `IP` not `localhost` nor `127.0.0.1` and from `Android` make the call to that `IP` and don't forget to make sure that `Android` device is on the same network with the `Node.js` server. – Eslam El-Meniawy Jan 13 '16 at 10:56
  • @EslamEl-Meniawy I am already using my system's IP in URL to access node.js server i.e http://198.164.1.24:3000/api/v1/object but still it is refusing connection. I am sure that my system and my phone both are on same network and I have disabled system's firewall also. – vishal gaurav Jan 13 '16 at 11:14
  • @vishalgaurav Then I guess you need to add code to your question for people to see what exactly you are doing and if there is a problem with the code. – Eslam El-Meniawy Jan 13 '16 at 11:23

3 Answers3

3

I made server listen to hostname '0.0.0.0'. Initially I was not providing any hostname as argument in server.listen i.e it was like server.listen('portnumber') which I changed to server.listen('portnumber', '0.0.0.0');

vishal gaurav
  • 686
  • 1
  • 9
  • 24
1

if you are using android emulator and trying to access the the host machine's

  • local host use IP address : 10.0.2.2
  • for genymotion use IP address: 10.0.2.3

this should allow you to access the host machine's local host, but you will have to change this IP address to local IP address when running app on a device and connected to same network, to access from other network you need to open port(port forwarding I guess) in your router and talk ask your ISP to do the same.

here's a link to similar question : here

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
0

I was facing the same kind of issues

  • I wasn't able to call the API from emulator. I made it working by replacing localhost with 10.0.2.2:YOUR_PORT_NUMBER

  • I was able to call from an external device using ngrok (https://ngrok.com/)

  • If you want to call api without using external services go through this article may be helpful Link

Prajwal H N
  • 59
  • 1
  • 2