将HttpClient响应转换为JsonObject时出错


Error converting HttpClient response to JsonObject

我正在做一个Android项目,我正在让我的Android应用程序发布到PHP REST web服务,然后这个web服务返回JSON供Android应用程序处理。

该应用程序所做的是允许用户从他们的android应用程序管理MySQL数据库,因此我在数据库上运行查询并返回JSON,但我得到了JSON异常。下面是我用来发布到服务器并获取响应的代码。

    new Thread(new Runnable() {
                @Override
                public void run() {
                    try
                    {
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpPost httpPost = new HttpPost(serverUrl);
                        if (postData != null)
                        {
                            httpPost.setEntity(new UrlEncodedFormEntity(postData));
                        }
                        ResponseHandler<String> responseHandler = new BasicResponseHandler();
                        String responseBody = httpClient.execute(httpPost, responseHandler);
                        Log.d(TAG + " Response", responseBody);
                        ServerResultProcessor serverResultProcessor = new ServerResultProcessor(progressDlg);
serverResultProcessor.processExecuteSqlQuery(iQueryExecution, 
                                new JSONObject(responseBody));

它在new JSONObject(responseBody)上抛出异常。我知道这是正常的,因为我成功地处理了其他JSON,它只是在应用程序的这一部分不工作。

下面是我如何将JSON从PHP返回到Android 的代码

function executeQuery($postData)
        {
            include_once ("ConnectionManager.php");
            $connManager = new ConnectionManager();
            $status = $connManager->connectToDBFromPostArray($postData);
            if ($status[RESULT] != SUCCESS)
            {
                print json_encode($status);
                exit();
            }
            $result = mysql_query(mysql_escape_string($postData['query']));
            if ($result)
            {
                $data = array();
                while ($myrow = mysql_fetch_array($result))
                {
                    $data[] = $myrow;
                }
                print json_encode($data);
            }
            else
            {
                $status = array();
                $status[RESULT] = ERROR;
                $status[MYSQL_ERROR] = mysql_error();
                $status[ERROR_NO] = mysql_errno();
                print json_encode($status);
            }
        }

下面是从logcat 返回的JSON

12-16 00:33:41.06:D/PostToApi响应(8513 3":"6.0.1.9","版本":"6.0.1.9"},{"0":"3","id":"3","1":"1","SoftwareID":"一","2":"1","PlatformID":"1","3":"6.0.2.0","版本":"6.0.2.0"},},"0":"4","id":"4","1

以下是例外:

12-16 00:33:41.110:E/PostToApi(8513):org.json.json异常:值[{formID":"1","版本":"6.0.1.9","软件id":"1"},{"3":"6.0.2.0","id":"3","2":"1",org.json.JSONArray类型的"id":"4"、"2":"1"、"1":"0":"3":"6.1.0.0"、"id"5"、"3"ONObject

我看不出这有什么不对。

问题是您的JSON

[{"0":"1","id":"1","1":"1","SoftwareID":"1","2":"1","PlatformID":"1","3":"6.0.1.8","Version":"6.0.1.8"},{"0":"2","id":"2","1":"1","SoftwareID":"1","2":"1","PlatformID":"1","3":"6.0.1.9","Version":"6.0.1.9"},{"0":"3","id":"3","1":"1","SoftwareID":"1","2":"1","PlatformID":"1","3":"6.0.2.0","Version":"6.0.2.0"},{"0":"4","id":"4","1":"1","SoftwareID":"1","2":"1","PlatformID":"1","3":"6.0.2.1","Version":"6.0.2.1"},{"0":"5","id":"5","1":"1","SoftwareID":"1","2":"1","PlatformID":"1","3":"6.1.0.0","Version":"6.1.0.0"}]

是一个数组(注意前面的[),您正试图将其用作JSON对象。

使用

new JSONArray(responseBody);