我用下面的代碼使用PHPPHP意想不到的JSON發送
<?php
$host = "localhost";
$user = "**MASKED**";
$password = "**MASKED**";
$database = "parkfinder_zxq_coordinates";
$connection = mysql_connect($host, $user, $password) or die("couldn't connect to server");
$db = mysql_select_db($database, $connection) or die("couldn't select database.");
//$request_parked = $_REQUEST['parked'];
$request_long = $_REQUEST['longtitude'];
$request_lat = $_REQUEST['latitude'];
$q = mysql_query("SELECT * FROM Coordinates");
while ($e = mysql_fetch_assoc($q))
$output[] = $e;
print (json_encode($output));
mysql_close();
?>
當我看到在Java中使用此代碼的響應來訪問我的DB:我收到
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://parkfinder.zxq.net/default.php");
httppost.setEntity(new UrlEncodedFormEntity(coordinatesToSend));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
Log.d("RESULT", result);
JSONObject json_data = new JSONObject(result);
其結果是:
2[{"longtitude":"32.32","latitude":"33.12"}]
這將導致的JSONObject拋出異常,因爲這不是有效的JSON數據,它以2開始 有誰知道如何解決它?這兩個代碼都來自網上的教程,這似乎沒有這種問題。問題發生在兩個不同的MySQL服務器..
在此先感謝
真的嗎? Java與PHP,你把* PHP *的一面?!無論如何,這裏的問題是在PHP端,你可以通過訪問這個URL看到:'http:// parkfinder.zxq.net/default.php' –
肯定想從downvoter的解釋.. –