java 从 Android 模拟器连接到 LocalHost/10.0.2.2 超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35441481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:09:29  来源:igfitidea点击:

Connection to LocalHost/10.0.2.2 from Android Emulator timed out

javaphpandroidpostlocalhost

提问by Saumik Bhattacharya

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.

虽然这个问题在 StackOverflow 中被问过多次,我也经历过很多次,但我仍然无法解决我的问题,或者我无法找出问题的根本原因。因此,发布一个新问题。

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?

  1. 如何从 Eclipse 中的 Android Emulator 连接到我的 http://localhost Web 服务器

  2. 从 Android 模拟器访问 localhost:port

  3. 如何从我的 Android 设备访问我的本地主机?

  4. 如何在android模拟器中连接本地主机?

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

我正在尝试通过 WAMP 服务器使用 PHP 将 Android 与 MySQL 数据库连接起来。PHP 文件 (user_registration.php) 保存在以下路径中——

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)".

现在在执行代码后,我收到类似“ 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

Android 连接到本地主机

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.

在这种情况下,你能帮我吗?请注意,我使用 Android Studio 进行构建。

Thanks in advance!

提前致谢!

采纳答案by Saumik Bhattacharya

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.2to the IPv4address - which can be obtained on windows by typeing ipconfigin the command prompt and see linkfor linux.

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

  1. IP 地址不正确。因此,我将 IP 地址从10.0.2.2更改为IPv4地址 - 可以在 Windows 上通过ipconfig在命令提示符下键入并查看linux链接来获取该地址。

  2. 端口号 8080 也不正确。我已经在httpd.conf文件中设置了我自己的端口号,比如 ##Listen 12.34.56.78:8383Listen 0.0.0.0:8383Listen [::0]:8383##,在 Apache 下,我使用了相同的端口号。

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

更改两者并重新启动 WAMP 服务器后,它就像一个魅力。

回答by styssi

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.

仅在 Google Android 虚拟设备上使用 10.0.2.2 从您的 Android 连接到您的计算机。Android 虚拟设备正在侦听 10.0.2.2 并将所有请求转发到您的计算机。

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

Genymotion Android 虚拟设备正在侦听 10.0.2.3 并将这些请求转发到您的计算机。

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.

10.0.2.2 不适用于您的真实 Android 设备。如果你想在你的真实设备上使用它,你必须设置你的计算机的 IP,正如之前的答案所建议的那样。

回答by Zafar Imam

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

为什么要在模拟器或真实设备上运行 localhost 或任何 ip 地址,只需使用ngrok以非常方便和简单的方式将 localhost 转换为全局地址。