116

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?

1

10 Answers 10

334

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

Sign up to request clarification or add additional context in comments.

11 Comments

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 localhost:8080/aero
I got it, we have to chane the path given in the application like http://localhost:8080/link/to/resource.xml to http://10.0.2.2:8080/link/to/resource.xml
@VikasPatidar can u please tell me where to change this address to 10.0.2.2 in my code as am a newbie
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
Replace 10.0.2.2with 10.0.3.2 if you are running on Genymotion.
|
34

in android

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

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

Good luck!

4 Comments

where in which file ?
where you have your base URL, may inside constant class or retrofit class
@PravinDesai you can add BaseURL in gradle. stackoverflow.com/questions/55518814/…
@PhuocLuong Thank you, I didn't know that's possible too.
23

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.

4 Comments

can u explain that more in detail, do need to chane my machines IP address
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 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.
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.
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"

Comments

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

Comments

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

Comments

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

Comments

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

Comments

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!

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.