Connection to LocalHost/10.0.2.2 from Android Emulator timed out
Asked Answered
G

6

19

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)
    {
        try
        {
            HttpURLConnection connection = null;

            URL url = new URL("http://10.0.2.2:8080/android_connect/user_registration.php");
            connection = (HttpURLConnection)url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setConnectTimeout(720000000);
            connection.setReadTimeout(72000000);
            connection.connect();

            OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
            output.write(emp_details.toString());
            output.flush();
            output.close();

            HttpResult = connection.getResponseCode();

            connection.disconnect();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        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 --

C:\wamp\www\android_connect

Now after executing the code, I am getting an error like "java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (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!

Gustaf answered 16/2, 2016 at 19:31 Comment(0)
G
12

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 10.0.2.2 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 12.34.56.78:8383Listen 0.0.0.0:8383Listen [::0]:8383##, under Apache and I used the same.

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

Gustaf answered 18/2, 2016 at 16:16 Comment(5)
Could you explain "So I changed the IP Address from 10.0.2.2 to the IPv4 address"? ThanksDustcloth
I was using 10.0.2.2 as my IP address in my code, which was incorrect. Hence I changed it to the IPv4 address. Then it started working.Gustaf
What is the IPv4 address?Dustcloth
@domji84 -- Go to Command Prompt, and type ipconfig, it will list out the IP addresses. There you can find IPv4 address.Gustaf
Do you need to open the port on windows firewall when using IPv4 for USB debugging? need private network to find the computer to connect to?Costmary
F
11

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

Genymotion Android Virtual Devices are listening on 10.0.2.3 and forwarding those requests to your computer.

10.0.2.2 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.

Flirt answered 17/11, 2016 at 19:20 Comment(1)
It’s 10.0.3.2 for recent versions of Genymotion, see https://mcmap.net/q/35807/-access-host-from-genymotion-emulatorHaemostatic
I
1

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.

Intermarriage answered 29/8, 2018 at 11:8 Comment(0)
D
1

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 10.0.2.2 is added to bypass without mask
  • that you didn't make a typo, sometimes people put 0 instead of 2 like this 10.0.0.2:8080
Damage answered 26/6, 2021 at 10:55 Comment(1)
The issue got solved by disabling proxy. .. Btw, I couldnt find where to add the IP address 10.0.2.2 to bypass it. Pls share where to add this in the settings.Sural
G
0

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

private static final String BACKEND_URL = "http://10.0.2.2:4242/"; // 10.0.2.2 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 "http://10.0.2.2:4242/" didn't work. Solution was to change "http://10.0.2.2:4242/" to "https://<IP_OF_SERVER>:4242/"

Gladysglagolitic answered 26/2, 2021 at 0:13 Comment(0)
P
0

This may be caused by vulkan. To fix it, you must turn vulkan off on emulator or chrome.

check out this answer.

https://mcmap.net/q/277594/-google-chrome-browser-in-android-12-emulator-doesn-39-t-load-any-webpages-internet-is-working

Pym answered 8/5, 2023 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.