从 Android 模拟器连接到 LocalHost/10.0.2.2 超时 [英] Connection to LocalHost/10.0.2.2 from Android Emulator timed out
问题描述
虽然这个问题在 StackOverflow 中被问过多次并且我经历了很多,但我仍然无法解决我的问题,或者我无法找出问题的根本原因.因此,发布一个新问题.
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 --
从 Android 模拟器访问 localhost:port
这是我的代码 --
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;
}
我正在尝试通过 WAMP 服务器使用 PHP 将 Android 与 MySQL 数据库连接起来.PHP 文件 (user_registration.php) 保存在以下路径 --
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
现在执行代码后,我收到类似java.net.SocketTimeoutException: failed to connect to/10.0.2.2 (port 8080) after 720000000ms: isConnected failed: ETIMEDOUT (Connection timed out)".
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 --
但是不明白怎么解决的!:)
But could not understand how it has been resolved! :)
在这种情况下你能帮我吗?请注意,我使用 Android Studio 进行构建.
Can you please help me in this case? Please note I am using Android Studio for my build.
提前致谢!
推荐答案
我已经找到了它不工作的原因.有两个问题——
I have figured out the reason why it was not working. There were two issues --
IP 地址不正确.因此,我将 IP 地址从
10.0.2.2
更改为IPv4
地址 - 在 Windows 上可以通过在命令提示符中键入ipconfig
和请参阅 链接(适用于 Linux).
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 link for linux.
端口号 8080 也不正确.我在 httpd.conf
文件中设置了我自己的端口号,比如##Listen 12.34.56.78:8383Listen 0.0.0.0:8383Listen [::0]:8383##
,我在Apache下用的一样.
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.
更改两者并重新启动 WAMP 服务器后,它工作得很好.
After changing both and re-starting the WAMP server, it worked like a charm.
这篇关于从 Android 模拟器连接到 LocalHost/10.0.2.2 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!