我正在开发一个简单的 PHP 页面来生成 JSON 文本,这样我就可以测试我的应用程序(实际的服务器正在由另一个人开发),但我遇到了一个奇怪的错误。我的 PHP 页面只有这个:
<?php
header('Content-type: application/json');
$trip = array ('trip' => array ( array ('departureStation' => $_GET['from'],
'arriveStation' => $_GET['to'],
'departureTime' => '08:00',
'arriveTime' => '11:00',
'date' => $_GET['date'],
'duration' => '3',
'distance' => '80',
'price' => '5',
'changeLine' => false,
'waitTime' => '0',
'passengers' => '13'),
array ('departureStation' => $_GET['from'],
'arriveStation' => $_GET['to'],
'departureTime' => '11:00',
'arriveTime' => '14:00',
'date' => $_GET['date'],
'duration' => '3',
'distance' => '80',
'price' => '5',
'changeLine' => false,
'waitTime' => '0',
'passengers' => '29'),
array ('departureStation' => $_GET['from'],
'arriveStation' => $_GET['to'],
'departureTime' => '17:00',
'arriveTime' => '20:00',
'date' => $_GET['date'],
'duration' => '3',
'distance' => '80',
'price' => '5',
'changeLine' => false,
'waitTime' => '0',
'passengers' => '45')));
echo json_encode($trip);
?>
我检查并返回一个有效的 JSON,但是当我这样做时
URL url = new URL("http://xxx.xxx.x.xx/consult.php" + param);
con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000); /* milliseconds */
con.setConnectTimeout(15000); /* milliseconds */
con.setRequestMethod("GET");
con.setDoInput(true);
con.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8" ));
payload = reader.readLine();
有效载荷变量只得到一个<br />
我知道那是 PHP 上的东西,因为我在页面上输入,复制了输出,再次回到 PHP 代码,然后放
$echo 'json_copied_from_the_page_here';
并且有效,有效负载正在正确读取页面。所以我很好奇,为什么会这样?