无法在 Android 中解析 JSON 响应


Unable to parse JSON reponse in Android

我知道这个问题很常见,但提出的所有解决方案似乎都不适合我的情况。我正在尝试从 PHP 网络服务检索 JSON 响应,但不断收到此错误

Error parsing data org.json.JSONException: Value <br of type java.lang.String
W/System.err: org.json.JSONException: No value for success

而下面是我认为与错误相关的我的Android代码

        // getting JSON response from PHP web service
JSONObject returnedJSONObj = listsJSONParser.makeHttpRequest(Constant.URL
                + "RetrieveListSVC.php", "GET", params);
Log.d("Returned JSON ", String.valueOf(returnedJSONObj));
success = returnedJSONObj.getInt(Constant.TAG_SUCCESS);
Log.d("JSON Success Value", String.valueOf(success));
if (success == 1){
// do stuff}

在日志Returned JSON我在 LogCat 中收到的输出是

D/Returned JSON: {"androidid":"1"}

但是日志JSON Success Value没有输出。如第一块所示,成功没有价值。PHP已经给了我,我没有编写它,因为我对PHP几乎一无所知,所以服务可能是有问题的服务。这是可能相关的代码

<?php
define('IEM_PATH', '../admin/com');
require_once('../admin/includes/config.php');
require_once('../admin/com/lib/IEM.class.php');
require_once ('../admin/com/lib/IEM/DBFACTORY.class.php');
require_once ('../admin/com/lib/IEM/baseAPI.class.php');
require_once ('../admin/com/lib/API/USERS.class.php');
require_once ('../admin/com/lib/IEM/baseRecord.class.php');
require_once ('../admin/com/lib/record/Users.class.php');
    function GetLists($userid = 0, $getUnconfirmedCount = false) {
        $userid = $_REQUEST['userID'];
        if (!$userid) {
        trigger_error('This user object is not loaded with any user.... You will need to supply the userid as a parameter.', E_USER_NOTICE);
            return false;
        }

        if (!$userid) {
            $userid = $this->userid;
        }
        // If user is a "System Admin" or a "List Admin", allow to access all lists
      /*  if ($userid == $this->userid) {
            if ($this->ListAdmin() || $this->listadmintype == 'a') {
                $userid = 0;
            }
        }
      */
        require_once('../admin/functions/api/lists.php');
        $listapi = new Lists_API();
        $returnA =  $listapi->GetListByUserID($userid, $getUnconfirmedCount);
        $returnResult1 = array();
            foreach ($returnA as $key => $value) { 
                //$lists[] = $key;
                $returnResult["contactList"][] = array("listID" => $returnA[$key]['listid'], "name" => $returnA[$key]['name']);
            }
       $returnResult["success"] = 1;
       echo json_encode($returnResult) ;
    }
    //}
    GetLists();

我提问的目的是我想知道问题出在 Java 还是 PHP 代码上?我是否正确处理了 JSON 响应,还是应该以另一种方式处理。

JSON 响应中存在错误

JSON 对象中没有关键的"成功"。

如我所见,只有安卓id作为{"androidid":"1"}的关键。

Constant.TAG_SUCCESS="androidid";

您得到的是字符串而不是整数。错误在下面的语句中

success = returnedJSONObj.getInt(Constant.TAG_SUCCESS);

它应该是

success = returnedJSONObj.getString(Constant.TAG_SUCCESS);

您分配给Constant.TAG_SUCCESS的值是错误的,其中提到了success

因此,将Constant.TAG_SUCCESS的值更改为"androidid or make another variable as androidid and assign it a value as androidid" 这将为您带来所需的输出。

和改变

success = returnedJSONObj.getInt(Constant.TAG_SUCCESS);

success = returnedJSONObj.getString(yourNewVariable); //new variable having value which is your json attribute