0

I have setup WAMP on my computer and have created a PHP web service which is located at this URL

http://localhost/WebService/?mode=get

I am using Android Studio and running the project on my Nexus 5 phone. Both the development computer and my phone are connected to the same wifi network.

When I try and call that URL from the application I get an error:

Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)

In my manifest I have the internet permission.

How can I access a local web service from my attached phone for dev/debug?

andrewb
  • 2,995
  • 7
  • 54
  • 95

3 Answers3

2

Emulator uses a special ip address to access the host machine local host. Use this link

http://10.0.2.2/WebService/?mode=get

In some cases you might have to assign a port other than default.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

Check your URL from your emulator or from the device... if you are not able to access it, Find the right url....

Open command prompt->type ipconfig-> Get IPV4 address.... something like 192.xx...

enter image description here

So your URL is "http://192.xx.xx.xx/WebService/?mode=get"

To access your local host from your android emulator Use the ip address given in ipconfig.

Indra Kumar S
  • 2,818
  • 2
  • 16
  • 27
  • If your machine ip address changes you will have to change the code. Android provides a special ip address which is <10.0.2.2> This is used to access host machine irrespective of the ip of the host machine. So no use of doing all this extra steps. If the user is tring to access on a device not emulator then it would work. – Rohit5k2 Jan 05 '15 at 16:57
  • I have been using for the past two years .... You mean to say My machine ip address never changed? You try this method If it doesnt work then come and tell us.. – Indra Kumar S Jan 05 '15 at 16:59
  • And see this post http://stackoverflow.com/q/3378501/3577946 http://10.0.2.2 has not worked and suggestion was to use 192.XX.XX – Indra Kumar S Jan 05 '15 at 17:01
  • Try to use a public network where they provide non static ip; not your home wifi. And there is no need of taking it personal. See this http://developer.android.com/tools/devices/emulator.html#emulatornetworking – Rohit5k2 Jan 05 '15 at 17:03
  • The question you are referring is for android device not emulator. there is a difference, you see. – Rohit5k2 Jan 05 '15 at 17:04
  • I am using it in both emulator and devices – Indra Kumar S Jan 05 '15 at 17:04
  • So using my IP address (192.168.x.x) worked and the 10.0.2.2 did not work, although that is maybe because of the port needing specified? I can access 192.168.x.x/webservice from the app and my phone's browser. Thanks – andrewb Jan 05 '15 at 17:05
  • Try somewhere where your ip is changed. Probably outside your home network or use a mobile network on your device not wifi. I will wait for your comment then. – Rohit5k2 Jan 05 '15 at 17:06
0

It might be best to use

machine_name.local

as the host name, as your server/computer could change IP address if you are using DHCP

cmorrissey
  • 8,493
  • 2
  • 23
  • 27