Volley 的 JsonObjectRequest 给出 "org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject" 异常

Volley's JsonObjectRequest gives "org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject" exception

预期 JSON 发送到服务器

{"syncsts":
          [
          {"status":"1","Id":"9"},
          {"status":"1","Id":"8"}
          ]
}

服务器上 JSON 对象的预期检索方式

$arr = $_POST['syncsts']

(我认为)

$arr 应该有 [{"status":"1","Id":"9"},{"status":"1","Id":" 8"}]

Volley JsonObjectRequest 函数以及 JSON 样式参数

public void updateMySQLSyncSts(final ArrayList<HashMap<String, String>> lt){
    JSONArray arr = new JSONArray(lt);
    JSONObject js = new JSONObject();
    try {
        js.put("syncsts",arr);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.i("MainActivity",js.toString());
    String url = "http://10.0.3.2/insight/mysqlsqlitesync/updatesyncsts.php";
    JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, js,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    Toast.makeText(getApplicationContext(), "MySQL DB has been informed about Sync activity", Toast.LENGTH_LONG).show();
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i("MainActivity",error.getMessage());
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    MySingleton.getInstance(this).addToRequestQueue(req);
}

PHP 脚本

/**
* Updates Sync status of Users
*/
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions(); 
//Get JSON posted by Android Application
$json = $_POST["syncsts"];
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into MySQL DB
$res = $db->updateSyncSts($data[$i]->Id,$data[$i]->status);
//Based on inserttion, create JSON response
if($res){
    $b["id"] = $data[$i]->Id;
    $b["status"] = 'yes';
    array_push($a,$b);
}else{
    $b["id"] = $data[$i]->Id;
    $b["status"] = 'no';
    array_push($a,$b);
}
}
//Post JSON response back to Android Application
echo json_encode($a);

我正在使用上述函数发送 JSON 格式的参数以及我的 post 请求。当执行 PHP 脚本中的 $_POST["syncsts"] 行时,抛出以下错误。

JSONObject toString() 和 logcat

中所示的错误
10-04 21:59:23.523 2062-2062/? I/MainActivity: {"syncsts":[{"status":"1","Id":"9"},{"status":"1","Id":"8"}]}

10-04 21:59:23.767 2062-2062/? I/MainActivity: org.json.JSONException: Value    <br of type java.lang.String cannot be converted to JSONObject

PHP 显示服务器接收到的内容的脚本(调试)

file_put_contents('test.txt', file_get_contents('php://input'));
$arr = $_POST['syncsts'];
echo $arr;
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo json_encode($age);

test.txt

{"syncsts":[{"status":"1","Id":"9"},{"status":"1","Id":"8"}]}

请告诉我在 Java.

中生成的 JSON 格式我试图发送到我们的服务器有什么问题

看起来错误来自服务器的响应。 onResponse(JSONObject response) 期望结果是正确的 jsonobject。检查回显结果后是否显示任何其他元素(例如错误消息)。您可以使用名为 postman 的 chrome 扩展从浏览器中检查它,您可以尝试使用它手动发送 POST 和 GET 请求到服务器