0

Edit:

As I've just seen, it happens even with the simplest setup:

InputStream stream = new URL("http://xx.xx.xxx.xxx/GetAll.php").openStream();

Gives the same timeout error. I think I'm missing some basic configuration.


I used HTTPGet to connect to a PHP web service I have. I saw it's deprecated so I've been trying to switch to the recommended HttpUrlConnection but with no success.

The HttpURLConnection does not seem to be able connect to the service, even though I can connect from my web browser without any problem.

My connection code:

URL myUrl = new URL("http://xx.xx.xxx.xxx/GetAll.php");
HttpURLConnection request = (HttpURLConnection)myUrl.openConnection();
request.setRequestProperty("Content-Type","text/xml;charset=UTF-8");
InputStream stream = request.getInputStream();

The GetAll.php file:

<?
require_once('MysqliDb.php'); //Helper class
$db = new MysqliDb();

//All closest events by date
$All = $db->query("SELECT * FROM Event;");

//Return in JSON
echo json_encode($All);

The result I am getting from the file:

[{"EventID":1,"StartTime":1300,"Duration":1,"EventDate":"2015-05-17","EventOrder":1,"Type":0,"Name":"\u05e2\u05d1\u05e8\u05d9\u05ea AND ENGLISH","Organiser":"Neta","Phone":"012345678","Location":"Loc","Description":"Desc"}]

Thank you,

Neta

Neta
  • 871
  • 5
  • 14
  • 30
  • [You really can't find a more complete guide than this answer here](http://stackoverflow.com/a/2793153/1206267). But like he says, *You'll need to handle trivial IOExceptions and RuntimeExceptions like NullPointerException, ArrayIndexOutOfBoundsException and consorts yourself.* – Ohgodwhy Mar 17 '15 at 20:17
  • Well, I don't think it has anything to do with it. The guide shows a similar setup to mine, and as I've seen just now (I'll edit it in my question in a moment), it happens without the HttpUrlConnection too. – Neta Mar 17 '15 at 21:01

1 Answers1

0

I want to share my solution, as this has cost me hours of hair tearing.

As it turns out, "Timed out" exception has nothing to do with the code, it's a network connectivity issue. The phone I used to debug the app sometimes appears to be connected to Wifi even though it really isn't.

Anyway, if you have this exception, try checking your network connection. Good luck!

Neta
  • 871
  • 5
  • 14
  • 30