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

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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 http://10.0.2.2:8080/ Because Android emulator runs in a Virtual Machine therefore here 127.0.0.1 or localhost will be emulator's own loopback address.

Refer: Emulator Networking

Solution 2 - Android

in android

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

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

Good luck!

Solution 3 - Android

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 4 - Android

  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"

Solution 5 - Android

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

Solution 6 - Android

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

Solution 7 - Android

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

Solution 8 - Android

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 .

  1. 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 9 - Android

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!

Solution 10 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpradeepView Question on Stackoverflow
Solution 1 - AndroidVikas PatidarView Answer on Stackoverflow
Solution 2 - AndroidPhuocLuongView Answer on Stackoverflow
Solution 3 - AndroidskorulisView Answer on Stackoverflow
Solution 4 - AndroidOmid RostamiView Answer on Stackoverflow
Solution 5 - AndroidMr.MoustardView Answer on Stackoverflow
Solution 6 - AndroidBABU KView Answer on Stackoverflow
Solution 7 - AndroidlotfiView Answer on Stackoverflow
Solution 8 - AndroidShohel RanaView Answer on Stackoverflow
Solution 9 - AndroidancabView Answer on Stackoverflow
Solution 10 - AndroidMehulView Answer on Stackoverflow