简体   繁体   English

Java无法连接到PHP Web服务

[英]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. 我使用HTTPGet连接到已有的PHP Web服务。 I saw it's deprecated so I've been trying to switch to the recommended HttpUrlConnection but with no success. 我看到它已被弃用,因此我一直在尝试切换到推荐的HttpUrlConnection,但没有成功。

The HttpURLConnection does not seem to be able connect to the service, even though I can connect from my web browser without any problem. HttpURLConnection似乎无法连接到该服务,即使我可以从Web浏览器进行连接也没有任何问题。

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: GetAll.php文件:

<?
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. 我用来调试该应用程序的手机有时似乎已连接到Wifi,即使实际上并非如此。

Anyway, if you have this exception, try checking your network connection. 无论如何,如果您有此异常,请尝试检查您的网络连接。 Good luck! 祝好运!

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

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