0

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';

ペイロードはページを正しく読み取っています。どうしてこうなったのか気になりますよね?

4

1 に答える 1

1

1行しか読んでいないので、残りの出力はその行の後ろにあるはずです。

これを試して

編集: 文字列に += を使用することはできません。

String temp = null;
String output = "";
while ((temp = reader.readLine()) != null) {
    output = output + temp;
} // end while

動作する場合は出力を参照してください。

于 2012-11-09T11:14:04.093 に答える