将HttpClient响应转换为JsonObject时出错

时间:2013-12-16 00:41:28

标签: java php android mysql json

我正在开发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.106:D / PostToApi响应(8513):[{“0”:“1”,“id”:“1”,“1”:“1”,“SoftwareID”: “1”, “2”: “1”, “PlatformID”: “1”, “3”: “6.0.1.8”, “版本”: “6.0.1.8”},{ “0”: “2”, “ID”: “2”, “1”: “1”, “SoftwareID”: “1”, “2”: “1”, “PlatformID”: “1”, “3”: “6.0.1.9”, “版本”: “6.0.1.9”},{ “0”: “3”, “ID”: “3”, “1”: “1”, “SoftwareID”: “1”, “2”:“1 “ ”PlatformID“: ”1“, ”3“: ”6.0.2.0“, ”版本“: ”6.0.2.0“},{ ”0“: ”4“, ”ID“: ”4“,” 1 “:” 1" , “SoftwareID”: “1”, “2”: “1”, “PlatformID”: “1”, “3”: “6.0.2.1”, “版本”: “6.0.2.1”} ,{ “0”: “5”, “ID”: “5”, “1”: “1”, “SoftwareID”: “1”, “2”: “1”, “PlatformID”: “1”, “3”: “6.1.0.0”, “版本”: “6.1.0.0”}]

以下是例外情况:

  

12-16 00:33:41.110:E / PostToApi(8513):org.json.JSONException:Value [{“3”:“6.0.1.8”,“id”:“1”,“2”: “1”, “1”: “1”, “0”: “1”, “PlatformID”: “1”, “版本”: “6.0.1.8”, “SoftwareID”: “1”},{“3 “:” 6.0.1.9" , “ID”: “2”, “2”: “1”, “1”: “1”, “0”: “2”, “PlatformID”: “1”,“版“:” 6.0.1.9" , “SoftwareID”: “1”},{ “3”: “6.0.2.0”, “ID”: “3”, “2”: “1”, “1”:“1 “ ”0“: ”3“, ”PlatformID“: ”1“, ”版本“: ”6.0.2.0“, ”SoftwareID“: ”1“},{ ”3“: ”6.0.2.1“,” ID “:” 4" , “2”: “1”, “1”: “1”, “0”: “4”, “PlatformID”: “1”, “版本”: “6.0.2.1”,“SoftwareID “:” 1 “},{” 3 “:” 6.1.0.0" , “ID”: “5”, “2”: “1”, “1”: “1”, “0”: “5”, “PlatformID”:“1”,“Version”:“6.1.0.0”,“SoftwareID”:“1”}]类型org.json.JSONArray无法转换为JSONObject

我看不出有什么不妥。

1 个答案:

答案 0 :(得分:2)

问题是你的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);