i am making an application in android that has to read constantly a database hosted in my server, my question is, is better make a loop in android, reading the php document constantly or just read once but the php document makes a loop to get information from a database?
in android studio i just make a loop with a sleep, and read the php document. I don't have the code because i haven't made it, but here is the code to connect with php, just imagine it in a loop:
StringRequest conexion = new StringRequest(Request.Method.POST,
Constantes.URL_NombreIcono,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.charAt(response.length() - 1) == '1'){
}
else {
NombreGrupo.setText(response);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast("No se ha podido conectarse con la base de datos", "l");
}
});
RequestQueue requ = Volley.newRequestQueue(this);
requ.add(conexion);
and the php code in this case is like this:
<?php
$con = mysqli_connect('localhost','root','', 'JNDatabase');
if (!$con) {
echo "|Error";
}
else{
$query = "SELECT * FROM Log ORDER BY id DESC LIMIT 1";
$resultado = $con->query($query);
if (!$resultado){
echo "|Error";
}
else {
$row = $resultado->fetch_assoc();
if($row["Realizada"] == 1){
echo $row["Accion"]."|".$row["id"];
}
}
}
?>
The other case, is read the php document once, but the php document makes the loop, i have the code of the loop in php:
<?php
ini_set('max_execution_time', 0);
while (true) {
$con = mysqli_connect('localhost','root','', 'JNDatabase');
if (!$con) {
echo "|Error";
}
else{
$query = "SELECT * FROM Log ORDER BY id DESC LIMIT 1";
$resultado = $con->query($query);
if (!$resultado){
echo "|Error";
}
else {
$row = $resultado->fetch_assoc();
if($row["Realizada"] == 1){
echo $row["Accion"]."|".$row["id"];
}
}
}
ob_flush();flush();sleep(1);
}
?>
so which do you think is better,having in mind that is an android application and i have to take care about the battery.
PDT: I know that exists services like Firebase, but i want to do this with my own server, you know, try to avoid giving the information to other companies