1

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();
            }
3
  • Looks like you have an array where you're expecting an object. Could you post the whole JSON so we can be sure? Commented Aug 7, 2016 at 13:42
  • Seems like a similar scenario to this: stackoverflow.com/questions/12559612/…. Maybe it will help you. Commented Aug 7, 2016 at 13:43
  • Add name of your http library and post the full java method Commented Aug 7, 2016 at 14:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.