【发布时间】:2015-03-17 20:08:34
【问题描述】:
编辑:
正如我刚刚看到的,即使是最简单的设置也会发生这种情况:
InputStream stream = new URL("http://xx.xx.xxx.xxx/GetAll.php").openStream();
给出相同的超时错误。我想我缺少一些基本配置。
我使用 HTTPGet 连接到我拥有的 PHP Web 服务。 我看到它已被弃用,所以我一直在尝试切换到推荐的 HttpUrlConnection 但没有成功。
HttpURLConnection 似乎无法连接到该服务,即使我可以从我的网络浏览器毫无问题地连接。
我的连接代码:
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();
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);
我从文件中得到的结果:
[{"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"}]
谢谢,
内塔
【问题讨论】:
-
You really can't find a more complete guide than this answer here。但正如他所说,您需要自己处理琐碎的 IOExceptions 和 RuntimeExceptions,例如 NullPointerException、ArrayIndexOutOfBoundsException 和相关联。
-
好吧,我认为这与它无关。该指南显示了与我的类似设置,正如我刚才看到的(我稍后会在我的问题中对其进行编辑),它也会在没有 HttpUrlConnection 的情况下发生。
标签: java php json url httpurlconnection