-3

I use JsonObjectRequest in Android with PHP and MySQL and get below error message :

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

Below is my PHP code :

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

Somebosy say br is newline in html which is not in Json format ?

Beside, I use DateTime type or Timestamp type in MySQL "date" column. My Json response from PHP look like below. Is it about the MySQL setting or the PHP problem ? :

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

2 Answers2

1

I find my mistake. I Jsonrequest sending data form Android to PHP, but my JsonRequest setting is GET instead of POST, thus it return error message that "PHP doesn't get the value" instead of JsonArray. Thanks for your help ! I really appreciate it !

0

Don't convert every row in json encoded, add all row in a array and convert array into json, just like below

<?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();
?>
nageen nayak
  • 1,262
  • 2
  • 18
  • 28