JSON テキストを生成する単純な PHP ページを開発していたので、アプリケーションをテストできます (実際のサーバーは別の人によって開発されています)。奇妙なエラーに直面します。私の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 コードに戻って、
$echo 'json_copied_from_the_page_here';
ペイロードはページを正しく読み取っています。どうしてこうなったのか気になりますよね?