I am trying to pass a json array to my android studio so i can display data retrieve from mysql server however i keep getting this error:
W/System.err: org.json.JSONException: Value [{"id":"36","reportType":"Crime","address":"Republic Crescent","status":"pending"},] of type org.json.JSONArray cannot be converted to JSONObject
Any kind souls can help me solve this issue?
PHP Code:
$input = file_get_contents("php://input");
$jsonObj = json_decode($input, true);
$userid = $jsonObj['userId'];
$query_rsUser = sprintf("SELECT * FROM reports WHERE userid = %s",$userid);
$rsUser = mysqli_query($connDB, $query_rsUser);
while($row_rsUser = mysqli_fetch_assoc($rsUser)) {
$results[]=$row_rsUser;
}
echo json_encode($results);
mysqli_close($connDB);
?>
Java Code:
try {
JSONArray resultArray = jsonObject.getJSONArray("reportId");
reports.clear();
// Store all results into locations table layout
for (int i = 0; i < resultArray.length(); i++) {
JSONObject resultObj = resultArray.getJSONObject(i);
String reportid = resultObj.getString("id");
String reportType = resultObj.getString("reportType");
String address = resultObj.getString("address");
String status = resultObj.getString("status");
JSONObject ReportObj = resultObj.getJSONObject("resultObj");
reports.add(new ReportObj(reportid, reportType, address, status));
}
} catch (Exception e) {
e.printStackTrace();
}