简体   繁体   中英

Java Can't Connect To PHP Web Service

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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