[android] java.net.ConnectException: localhost/127.0.0.1:8080 - Connection refused

I am using xampp apache server to serve resources to the application from my machine. But i am getting the above error.

I got something on the google. pointing towards possible solution here http://groups.google.com/group/android-beginners/browse_thread/thread/599a06416fb37b4a

What is the solution for the above problem?

This question is related to android

The answer is


its working for me. I use genymotion for Run App.

1.Firstly i was checked my local ip. goto command mode>> And write ipconfig. Example: Windows 10>> search cmd>>then Write ipconfig . 2. Then get your local ip information >>> 3.Use give your localhost ip and virtual box ip. You need to use virtual box ip for genymotion.Check below screenshot. You can you below any ip under virtualbox host network enter image description here


You just have to use your local (but real) IP address and port number like this:

String webServiceUrl = "http://192.168.X.X:your_virtual_server_port/your_service.php"

And make sure you did set the internet permission within the manifest

<uses-permission android:name="android.permission.INTERNET" />


Replacing localhost with 10.0.2.2 is correct, but you can alsor replace localhost with your physical machine's ip(it is better for debug purposes). Ofc, if ip is provided by dhcp you would have to change it each time...

Good luck!


  1. Add Internet permission in Androidmanifest.xml file

uses-permission android:name="android.permission.INTERNET

  1. Open cmd in windows
  2. type "ipconfig" then press enter
  3. find IPv4 Address. . . . . . . . . . . : 192.168.X.X
  4. use this URL "http://192.168.X.X:your_virtual_server_port/your_service.php"

in android

Replace: String webServiceUrl = "http://localhost:8080/Service1.asmx"

With : String webServiceUrl = "http://10.0.2.2:8080/Service1.asmx"

Good luck!


localhost and 127.0.0.1 are both ways of saying 'the current machine'. So localhost on your PC is the PC and localhost on the android is the phone. Since your phone isn't running a webserver of course it will refuse the connection.

You need to get the IP address of your machine (use ipconfig on windows to find out) and use that instead of 127.0.0.1. This may still not working depending on how your network/firewalls are set up. But that is a completely different topic.


Solution is very simple.

1 Add Internet permission in Androidmanifest.xml file

<uses-permission android:name="android.permission.INTERNET" />

[2] Change your httpd.config file

Order Deny,Allow
Deny from all
Allow from 127.0.0.1

TO

Order Deny,Allow
Allow from all
Allow from 127.0.0.1

And restart your server.

[3] And most impotent step. MAKE YOUR NETWORK AS YOUR HOME NETWORK

Go to Control Panel > Network and Internet > Network and Sharing Center

Click on your Network and select HOME NETWORK

enter image description here


You just have to use your local IP address:using the cmd command "ipconfig" and your server port number like this:

String webServiceUrl = "http://192.168.X.X:your_local_server_port/your_web_service_name.php"

And make sure you did set the internet permission in your project manifest

It's working perfectly for me

Good Luck


If you are using localhost in your url and testing your application in emulator , simply you can replace system's ip address for localhost in the URL.or you can use 10.0.2.2 instead of localhost.

http://localhost/webservice.php to http://10.218.28.19/webservice.php

Where 10.218.28.19 -> System's IP Address.

or

http://localhost/webservice.php to http://10.0.2.2/webservice.php