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
Connection to LocalHost/10.0.2.2 from Android Emulator timed out
提问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 --
以下是我浏览过的链接列表——
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
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 --
我已经找出了它不起作用的原因。有两个问题——
The IP Address was not correct. So I changed the IP Address from
10.0.2.2
to theIPv4
address - which can be obtained on windows by typeingipconfig
in the command prompt and see linkfor linux.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.
IP 地址不正确。因此,我将 IP 地址从
10.0.2.2
更改为IPv4
地址 - 可以在 Windows 上通过ipconfig
在命令提示符下键入并查看linux链接来获取该地址。端口号 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,正如之前的答案所建议的那样。