I have mysql db and php script to get data from db. I cant get array of strings in my android app, textview is empty no matter what I do. I checked my php and it is actually working so i guess i have some error in java code in android:
bShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
showUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
try {
JSONArray students = response.getJSONArray("demo");
for (int i = 0; i < students.length(); i++) {
JSONObject student = students.getJSONObject(i);
String name = student.getString("name");
String lastname = student.getString("lastname");
String age = student.getString("age");
tvResult.append(name + " " + lastname + " " + age + "
");
}
tvResult.setText("===
");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.append(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
}
});
}
}
and my php file:
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include 'connection.php';
showStudent();
}
function showStudent()
{
global $connect;
$query = " Select * FROM demo; ";
$result = mysqli_query($connect, $query);
$number_of_rows = mysqli_num_rows($result);
$temp_array = array();
if($number_of_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$temp_array[] = $row;
}
}
header('Content-Type: application/json');
echo json_encode(array("students"=>$temp_array));
mysqli_close($connect);
}
?>
I tried php an mysql with postman and my guess is that im missing the obvious in java.
Any help is welcomed.