duancaiyi7567 2017-09-11 05:02
浏览 50

什么更好,在android中循环读取php或在php中读取数据库的循环?

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

  • 写回答

1条回答 默认 最新

  • dpa0760 2017-09-11 07:59
    关注

    In Android it depends what will you do with data received from your database. But definitely you shouldn't make loop in PHP. What it will do when Android device is off-line? PHP should get data from database (maybe from cache) and pass it to Android device by request.

    If you want to show data from database in UI and nothing else, you can use any loop or Handler.postDelayed() API. But would be nice if you will pause your work when application closed or paused.

    Or if you want do some work in background like storing data in SQLite you can use AlarmManager or JobScheduler API. There is a list of available tools.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度