English 中文(简体)
PHP意外发送JSON
原标题:PHP unexpected JSON sent

我使用以下代码使用PHP访问我的数据库

<?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中使用以下代码阅读响应时:

                    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"}]

which causes the JSONObject to throw an exception as this is not a valid JSON data, it starts with 2 Does anyone know how to fix it? both codes are taken from tutorials online, which didn t seem to have this kind of problem. the problem happened on two different MySQL servers..

提前感谢

最佳回答

检查你的PHP代码,确保在打印(json_encode($output))之前,你没有在某个地方回显“2”。这是唯一可能出错的事情。

顺便说一句,你应该确保在while循环之前声明$output=array()。否则,如果您没有结果行,您将得到意外的输出。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签