0

I need to get a table from my database and turn it into a java array of objects. To make this I used this php code:

 while(mysqli_stmt_fetch($statement)){  
    $response["success"] = true; 
    $response["tavolo"] = $tavolo;
    $response["pizza"] = $pizza;
    $response["quantita"] = $quantita;
    $response["note"] = $note;
    array_push($arr, $response);
}
echo json_encode($arr);

This should return for example this:

[{"success":true,"tavolo":"7","pizza":"Wurstel","quantita":"1","note":""},{"success":true,"tavolo":"9","pizza":"Wurstel","quantita":"1","note":""}]

The problem is how to turn each row into a java object. I thought I could use the same way as when I have only a row and I have to create only one object. For example (one row):

Response.Listener<String> responseListener = new Response.Listener<String>() {
     @Override
     public void onResponse(String response) {
        try {
           JSONObject jsonResponse = new JSONObject(response);
           Ordine o = new Ordine();
           o.setPizza(jsonResponse.getString("pizza"));
           o.setQuantità(jsonResponse.getString("quantita"));
           o.setNote(jsonResponse.getString("note"));
           t.getOrdini().add(o); //t contains an array of objects Ordine
           }catch (JSONException e) {
               e.printStackTrace();
           }
        }
   };
   OrdiniDelGiorno ordiniDelGiorno = new OrdiniDelGiorno(responseListener);
   RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
   queue.add(ordiniDelGiorno);

But the response to manage was only one row like this

{"success":true,"tavolo":"7","pizza":"Wurstel","quantita":"1","note":""}

And it worked, but when the rows are more like:

[{"success":true,"tavolo":"7","pizza":"Wurstel","quantita":"1","note":""},{"success":true,"tavolo":"9","pizza":"Wurstel","quantita":"1","note":""}]

which logic should I implement?

  • Yesterday i answered to a question and maybe you can find this useful [link](https://stackoverflow.com/questions/44601822/jsonobject-must-begin-with-at-character-1character-2-line-1/44606894#44606894) – Frank Jun 18 '17 at 15:37
  • @Frank I have read your answer but it didn't help me – Gioele Costa Jun 18 '17 at 15:56
  • If you run your backend on the browser , is it returning what you are expecting ? – Frank Jun 18 '17 at 16:12

0 Answers0