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.

8
  • what is your question? Commented Jan 13, 2016 at 9: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. Commented Jan 13, 2016 at 9: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. Commented Jan 13, 2016 at 9:34
  • Maybe you can replace 127.0.0.1 to *. because * stands for all ip address. Commented Jan 13, 2016 at 9:37
  • @BlackMamba That will not work. Commented Jan 13, 2016 at 10:37

3 Answers 3

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');

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.