Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
948 views
in Technique[技术] by (71.8m points)

java - Connection to LocalHost/10.0.2.2 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)
    {
        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:wampwwwandroid_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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...