0

Trying to add COUNT rows of MySQL table to ModelCLass, I searched almost all the method to be used and this the only one that made sense but cant understand the problem or where I went wrong if anyone can help that would be great .. .. thanks in advance

Error

 D/Volley: onError: com.androidnetworking.error.ANError: org.json.JSONException: Value {"count":4} of type org.json.JSONObject cannot be converted to JSONArray

php

$query = "SELECT ML.* FROM search ML INNER JOIN activity M ON M.activity_id = ML.activity_id  ORDER BY date_from ASC";
$sql = mysqli_query($conn, $query);
$num_rows = mysqli_num_rows($sql);
$output = array("count" => $num_rows);
$result = array(); 
while($row = mysqli_fetch_array($sql)){
array_push($result, array(

  'activity_img'=>$row[8],
  'company_name'=>$row[9],
  'user_id'=>$row[10]));
}
 echo json_encode($output);
 echo json_encode($result); 

java

 private void getData() {
    AndroidNetworking.get("http://10.0.2.2/Final/search.php")
            .setPriority(Priority.LOW)
            .build()
            .getAsJSONArray(new JSONArrayRequestListener() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, "onResponse: " + response);
                    {

                        try {
                            for (int i = 0; i < response.length(); i++) {
                                JSONObject data = response.getJSONObject(i);

                                promotModelClassList.add(new PromotModelClass(

                                        data.getString("activity_img"),
                                        data.getString("company_name"),
                                     data.getJSONArray("output").getJSONObject(0).getString("count")

                                ));

                            }

                            PromotAdapter adapter = new PromotAdapter(promotModelClassList, getContext());
                            promotRecycler.setAdapter(adapter);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                } @Override
                public void onError(ANError error) {
                    Log.d(TAG, "onError: " + error);
                }
            });
}
mohosyny
  • 962
  • 1
  • 9
  • 19
Meto Emeto
  • 21
  • 7
  • look at this : https://stackoverflow.com/q/12722468/9152105 – mohosyny May 17 '20 at 20:52
  • @mohosyny I checked the post and answers but I couldn't relate to my problem .. I get all the result from result array perfectly but when I tried to add another array which includes the count row I get the problem .. – Meto Emeto May 17 '20 at 21:03

1 Answers1

0

after improvising a lot I knew where I went wrong

so for php

$output = array("count" => $num_rows);
$result = array(); 
while($row = mysqli_fetch_array($sql)){
array_push($result, array(

'activity_img'=>$row[8],
'company_name'=>$row[9],
'user_id'=>$row[10]));
}

delete echo delete $output

  while($row = mysqli_fetch_array($sql)){
  array_push($result, array(

  'count'=>$num_rows,
  'activity_img'=>$row[8],
  'company_name'=>$row[9],
  'user_id'=>$row[10]));
  }

and since then I could get the count as string so I changed

  data.getJSONArray("output").getJSONObject(0).getString("count")

to

  data.getString("count")
Meto Emeto
  • 21
  • 7