Connection to LocalHost/ from Android Emulator timed out

Although this question has been asked multiple times in StackOverflow and I went through many of them, still I couldn’t resolve my issue or I am not able to find out the root cause of the issue. Hence, posting a new question.

Below are the list of links I went through —

  1. How to connect to my http://localhost web server from Android Emulator in Eclipse

  2. Accessing localhost:port from Android emulator

  3. How can I access my localhost from my Android device?

  4. how to connect localhost in android emulator?

Here goes my code —

protected Void doInBackground(Void... params)
            HttpURLConnection connection = null;

            URL url = new URL("");
            connection = (HttpURLConnection)url.openConnection();

            OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());

            HttpResult = connection.getResponseCode();

        catch(Exception e)
        return null;

I am trying to connect Android with MySQL database using PHP through WAMP server. The PHP file (user_registration.php) is being saved in the below path —


Now after executing the code, I am getting an error like “ failed to connect to / (port 8080) after 720000000ms: isConnected failed: ETIMEDOUT (Connection timed out)“.

I went through the particular link in order to resolve this issue —

Android connection to localhost

But could not understand how it has been resolved! 🙂

Can you please help me in this case? Please note I am using Android Studio for my build.

Thanks in advance!

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Making a connection from your Android to your Computer is working with only on an Google Android Virtual Device. Android Virtual Devices are listening for and forwarding all the requests to your computer.

Genymotion Android Virtual Devices are listening on and forwarding those requests to your computer. is not working with your real Android device. If you want to use it with your real device you have to set the IP of your computer, as it has been suggested by a previous answer.

Solution 2

I have figured out the reason why it was not working.
There were two issues —

  1. The IP Address was not correct. So I changed the IP Address from to the IPv4 address – which can be obtained on windows by typeing ipconfig in the command prompt and see link for linux.

  2. Also the port number 8080 was not correct. I have set my own port number in httpd.conf file, like
    ##Listen [::0]:8383##, under Apache and I used the same.

After changing both and re-starting the WAMP server, it worked like a charm.

Solution 3

Why to go for localhost or any ip address to run on emulator or real device simply go for ngrok to convert the localhost as a global address with very convenient and simplest way.

Solution 4

Accepted answer to this question is really unclear so maybe this will help someone.
Just got the same problem and resolved it by executing "adb reverse tcp:8080 tcp:8080", although after I revoked forwarding, the host machine remained visible and all ports were working, so it is unclear how it works.
Make sure that

  • you have no proxy in wifi settings
  • if you have, then is added to bypass without mask
  • that you didn’t make a typo, sometimes people put 0 instead of 2 like this

Solution 5

In my situation, I had this code running on my android device:

private static final String BACKEND_URL = ""; // is the Android emulator's alias to localhost

But my server code was running on my PC. So since my PC and my android device are "not the same device", that’s why "" didn’t work. Solution was to change "" to "https://<IP_OF_SERVER>:4242/"

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply