Home » Android » android – java.net.ConnectException: localhost/ – Connection refused

android – java.net.ConnectException: localhost/ – Connection refused

Posted by: admin March 11, 2020 Leave a comment


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

What is the solution for the above problem?

How to&Answers:

Since you have not specified you are connected to a server from the device or emulator so I guess you are using your application in the emulator.

If you are referring your localhost on your system from the Android emulator then you have to use Because Android emulator runs in a Virtual Machine therefore here or localhost will be emulator’s own loopback address.

Refer: Emulator Networking


in android

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

With : String webServiceUrl = ""

Good luck!


localhost and 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 This may still not working depending on how your network/firewalls are set up. But that is a completely different topic.


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" />


  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


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 instead of localhost.

http://localhost/webservice.php to

Where -> System’s IP Address.


http://localhost/webservice.php to


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


Replacing localhost with 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!


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


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


Order Deny,Allow
Allow from all
Allow from

And restart your server.
[3] And most impotent step.

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

Click on your Network and select HOME NETWORK

enter image description here