I'm making an android app that retrieves informations from a web page. 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. 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
- on physical device (as deployment target) => java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
For information, when I copy paste http://127.0.0.1:80/myPage.php in my browser, the access is granted.
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. I guess this is a matter of apache conf, but I have this, that seems correct to me :
<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 ?