简体   繁体   English

为什么我的应用程序无法使用 easyphp devserver 连接到 localhost?

[英]Why can't my app connect to localhost with easyphp devserver?

I'm making an android app that retrieves informations from a web page.我正在制作一个 android 应用程序,该应用程序从 web 页面检索信息。 In short, there's the code:简而言之,有代码:

    protected Void doInBackground(String... params) {
        HttpURLConnection conn = null;
        try {
            URL url;
            url = new URL(getHomeUrl() + "myPage.php");
            conn = (HttpURLConnection) url.openConnection();
            if( conn.getResponseCode() == HttpURLConnection.HTTP_OK ){
                doThings(conn);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(conn != null) {
                conn.disconnect();
            }
        }        
        return null;
   }

If the url is http://www.mywebsite.com/myPage.php , the connection is OK.如果 url 是http://www.mywebsite.com/myPage.php ,则连接正常。 But if the url is http://localhost/myPage.php or http://127.0.0.1:80/myPage.php with easyphp devserver on, I catch an IOException and get this: But if the url is http://localhost/myPage.php or http://127.0.0.1:80/myPage.php with easyphp devserver on, I catch an IOException and get this:

  • on android emulator => java.net.ConnectException: Connection refused在 android 模拟器 => java.net.ConnectException:连接被拒绝
  • on physical device (as deployment target) => java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)在物理设备上(作为部署目标)=> java.net.ConnectException:无法连接到 /127.0.0.1(端口 80):连接失败:ECONNREFUSED(连接被拒绝)

For information, when I copy paste http://127.0.0.1:80/myPage.php in my browser, the access is granted.有关信息,当我在浏览器中复制粘贴http://127.0.0.1:80/myPage.php时,授予访问权限。

I read that android emulator could "use" localhost, and it is suggested to take 10.0.2.2 instead, but it didn't work either.我读到 android 模拟器可以“使用”本地主机,建议使用 10.0.2.2 代替,但它也不起作用。 I guess this is a matter of apache conf, but I have this, that seems correct to me:我想这是 apache conf 的问题,但我有这个,这对我来说似乎是正确的:

<Directory "D:/Utilitaires/EasyPHP-Devserver-17/eds-www">
        Options FollowSymLinks Indexes ExecCGI
        AllowOverride All
        Order deny,allow
        Allow from 127.0.0.1
        Deny from all
        Require all granted
    </Directory>

Any idea?任何想法?

Your server is running on your computer, so 127.0.0.1 is working only on that machine.您的服务器正在您的计算机上运行,因此127.0.0.1仅在该计算机上运行。 If you would like to access it from your phone you should write your computer's local address (ie: 192.168.10.100 ).如果您想通过手机访问它,您应该写下您计算机的本地地址(即: 192.168.10.100 )。 It will be working fine if your network is configured properly.如果您的网络配置正确,它将正常工作。

I recommend to you that upload your website to a public host and you can easily access it from your mobile app.我建议您将您的网站上传到公共主机,您可以轻松地从您的移动应用程序访问它。

Got it !知道了 ! It failed with http://10.0.2.2 , but it's ok with http://10.0.2.2:80 ! http://10.0.2.2失败,但http://10.0.2.2:80 没问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM