【问题标题】:"JSONException: Value <br of type java.lang.String cannot be converted to JSONObject"? [duplicate]“JSONException:Java.lang.String 类型的值 <br 无法转换为 JSONObject”? [复制]
【发布时间】:2020-03-08 19:29:26
【问题描述】:

我在带有 PHP 和 MySQL 的 Android 中使用 JsonObjectRequest 并得到以下错误消息:

"JSONException: Value <br of type java.lang.String cannot be converted to JSONObject"

下面是我的 PHP 代码:

<?php

$month = $_POST['month'];

$connection = new mysqli("localhost","root","","Fubon");
$dateCheckSQLCommand = "select * from clockindata where Month(date)= $month  ";

if ($result = $connection->query($dateCheckSQLCommand)) {

    while ($row = $result->fetch_assoc()) {
        echo json_encode($row);
    }

  $result->free();

}

$connection->close();
?>

有人说 br 是 html 中的换行符,而不是 Json 格式?

此外,我在 MySQL 的“日期”列中使用 DateTime 类型或 Timestamp 类型。我对 PHP 的 Json 响应如下所示。是关于 MySQL 设置还是 PHP 问题? :

{"account":"Fu","ssid":"Fu","date":"2019-11-14 00:00:00"},{"account":"Fu","ssid":"Fu","date":"2019-11-21 00:00:00"}

【问题讨论】:

  • 我认为你需要先组合所有结果,然后再将其编码为 json 对象
  • 可以分享一下移动端收到的android代码和json数据吗??

标签: php android mysql json jsonobjectrequest


【解决方案1】:

我发现我的错误。我 Jsonrequest 从 Android 向 PHP 发送数据,但我的 JsonRequest 设置是 GET 而不是 POST,因此它返回错误消息“PHP 没有获取值”而不是 JsonArray。谢谢你的帮助 !我真的很感激!

【讨论】:

    【解决方案2】:

    不要将json编码中的每一行都转换为数组中的所有行并将数组转换为json,如下所示

    <?php
    
    $month = $_POST['month'];
    
    $connection = new mysqli("localhost","root","","Fubon");
    $dateCheckSQLCommand = "select * from clockindata where Month(date)= $month  ";
    $data = [];
    if ($result = $connection->query($dateCheckSQLCommand)) {
    
        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }
    
      $result->free();
    
    }
    echo json_encode($data);
    $connection->close();
    ?>
    

    【讨论】:

    • 谢谢!它是否通过使用您的代码成为 JsonArray ?但是我使用 JsonArrayRequest 并得到相同的错误消息...
    • 这是后端的正确方式,你需要自己做。
    猜你喜欢
    • 1970-01-01
    • 2017-01-10
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多