简体   繁体   中英

Error parsing JSON Object from PHP web service

I seem to be getting an incomplete JSON object from my PHP web service. My PHP codes are as follows:

<?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('User object has no user id as parameter.', E_USER_NOTICE);
            return false;
        }


        if (!$userid) {
            $userid = $this->userid;
        }

        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();

However when I try to retrieve the results my logcat only shows:

E/JSON Parser: Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
W/System.err: org.json.JSONException: No value for success

By doing a logging, my android returns my JSON object as follows

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

from these codes

// getting JSON response from PHP web service
JSONObject returnedJSONObj = listsJSONParser.makeHttpRequest(Constant.URL
                    + "RetrieveList.php", "GET", params);

Log.d("Returned JSON ", String.valueOf(returnedJSONObj));

success = returnedJSONObj.getInt("success");

I don't understand why there is no value for success when my PHP does return a JSON Array as well as the success value according to the code, but the android studio java codes does not detect these values in the JSON object. What am I doing wrong here?

you should modify your android code

success = returnedJSONObj.getInt("success");

to

success = returnedJSONObj.getInt("androidid");

The problem is that there is a problem with your php file, so instead of returning a JSON object, it returns a string with the error. You need to make sure you are sending the correct POST or GET values to the URL if you are or you check for database errors. try logging the inputstream coming from the php server to see the full error it is sending

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM