我正在開發一個簡單的PHP頁面來生成JSON文本,以便測試我的應用程序(實際的服務器正在由另一個人開發),並且我面臨一個奇怪的錯誤。我的PHP頁面僅僅是這樣的:Web服務獲取<br />而不是JSON
<?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';
和它的工作,有效載荷正確讀取頁面。所以我很好奇,爲什麼發生這種事?
您應該檢查例如與wireshark和模擬器你正在下載。 – rekire