110

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?

pradeep
  • 3,005
  • 12
  • 42
  • 65
  • 1
    Use 10.0.2.2 loopback to your localhost (127.0.0.1). You can read more about it here [Android Emulator Networking](https://developer.android.com/studio/run/emulator-networking.html) – Dmytro Melnychuk May 03 '17 at 14:43

10 Answers10

326

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

RealHandy
  • 534
  • 3
  • 8
  • 27
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
  • Yes , exactly i am doing the same thing. Can u explain in detail. Where to change the ip address to 10.0.2.2. This is the folder i am trying to access from xampp server http://localhost:8080/aero/ – pradeep Mar 31 '11 at 05:56
  • 1
    I got it, we have to chane the path given in the application like http://http://localhost:8080/link/to/resource.xml to http://http://10.0.2.2:8080/link/to/resource.xml – pradeep Mar 31 '11 at 06:06
  • @VikasPatidar can u please tell me where to change this address to 10.0.2.2 in my code as am a newbie – AndroidOptimist Jan 30 '14 at 06:58
  • 1
    e.g if your URL is like `http://localhost:8080/Getdata.php` or `http://127.0.0.1:8080/Getdata.php` then you have to change it as use `http://10.0.2.2:8080/Getdata.php` – Vikas Patidar Jan 30 '14 at 08:46
  • 1
    Replace `10.0.2.2`with `10.0.3.2` if you are running on Genymotion. – Jacob R Jul 23 '17 at 16:41
  • I do that from device `Socket socket = new Socket("127.0.0.1", 8000);` to send Socket from Android device to PC with ADB `adb forward tcp:8000 tcp:9000` and I get this error `W/System.err: java.net.ConnectException: failed to connect to /127.0.0.1 (port 8000): connect failed: ECONNREFUSED (Connection refused)` – user25 May 09 '18 at 20:13
  • Yeah it is working fine with emulator thanks buddy!. But when i am running on Android device with same network getting timeout exception. Please help if anyone know how to resolve it. – Zafar Imam Aug 29 '18 at 09:57
  • Additionally, If using Visual Studio (IIS Express), change `applicationhost.config` https://stackoverflow.com/questions/4831097/bad-request-invalid-hostname-iis7 – Jaider Mar 04 '19 at 05:54
  • Any idea how to handle this exception? I dont want my app to crash when the server does not run – Andre Thiele Dec 16 '19 at 08:51
  • Oh god that's useful! I always deployed my Server on Heroku for testing my Android Apps, because I thought there's no way to access localhost... haha – Boommeister May 26 '20 at 09:55
  • They keep moving things around from version to version, what point does it really serve? But thanks for your answer. – DragonFire Mar 04 '21 at 23:29
32

in android

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

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

Good luck!

Jack
  • 10,943
  • 13
  • 50
  • 65
PhuocLuong
  • 699
  • 9
  • 18
22

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.

skorulis
  • 4,361
  • 6
  • 32
  • 43
  • can u explain that more in detail, do need to chane my machines IP address – pradeep Mar 31 '11 at 05:21
  • 2
    If you type ipconfig it will tell you the IP address of your machine. This is most likely a private IP address for your network so it will only be accessible from devices that are on the same network so WIFI will probably work, 3G probably wont. – skorulis Mar 31 '11 at 05:29
  • @skorulis yes we need to set url of our machine which is different from 127.0.0.1 because we are connected to networks in our app code. – Ravi Yadav Jun 04 '16 at 07:14
  • You also can set a specific IP for your computer on which the server is running. Use this IP in your Android App. That's better because in the case you restart your router or computer you will get often a new IP for the computer and you would habe to change it in your code. – DOUBL3P Aug 16 '19 at 09:36
13
  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"
Omid Rostami
  • 524
  • 6
  • 10
9

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

Mr.Moustard
  • 1,297
  • 21
  • 24
5

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

BABU K
  • 917
  • 13
  • 35
4

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

lotfi
  • 425
  • 1
  • 6
  • 13
3

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

Shohel Rana
  • 2,332
  • 19
  • 24
2

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!

ancab
  • 780
  • 1
  • 7
  • 12
0

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

Mehul
  • 2,141
  • 1
  • 15
  • 15