从Android Emulator连接到LocalHost / 10.0.2.2超时 [英] Connection to LocalHost/10.0.2.2 from Android Emulator timed out

查看:543
本文介绍了从Android Emulator连接到LocalHost / 10.0.2.2超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在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 --


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

从Android模拟器访问localhost:端口

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

如何在android模拟器中连接localhost?

这是我的代码 -

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

我正在尝试使用PHP通过WAMP服务器连接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:\wamp\www\android_connect

现在执行代码后,我收到类似 java.net的错误.SocketTimeoutException:在720000000ms之后无法连接到/10.0.2.2(端口8080):isConnected失败:ETIMEDOUT(连接超时)

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

但无法理解它是如何解决的! :)

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


  1. IP地址不正确。所以我将IP地址从 10.0.2.2 更改为 IPv4 地址。

端口号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 Emulator连接到LocalHost / 10.0.2.2超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆