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?