从在线数据库中检索今天的数据

Retrieve todays data from an online database

我是 kinvey 的新手,我想从今天存储的在线数据库中获取结果,我希望我的应用程序每 10 秒刷新一次。

目前我可以从在线数据库中获取结果并以简单的形式显示,这是代码。

public void getData(String sensorid){   

        AsyncAppData<Sensor> mySensors = myJavaClient.appData("tdSensorData", Sensor.class);
        mySensors.getEntity("5514e7c06356be824801c207",new KinveyClientCallback<Sensor>(){

            @Override
            public void onFailure(Throwable arg0) {

                Toast.makeText(getApplicationContext(),arg0.getMessage(),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onSuccess(Sensor sensor) {

                String times = sensor.getDatetime();
                System.out.print(times);


                float f1 = Float.parseFloat(sensor.getSensorr1());              
                String sens1 = Float.toString(f1);              
                Senso1.setText(sens1);

                float f2 = Float.parseFloat(sensor.getSensorr2());              
                String sens2 = Float.toString(f2);
                Senso2.setText(sens2);

                float f3 = Float.parseFloat(sensor.getSensorr3());              
                String sens3 = Float.toString(f3);
                Senso3.setText(sens3);

                temper.setText(sensor.getTemp()+ " " + "°C");

            }

        });
    }

这是我的 GET 和 SET class

public class Sensor extends GenericJson{

    @Key("_id")
    private String id;

    @Key("DATETIME")
    private String datetime;

    @Key("MAC")
    private String mac;

    @Key("SENSOR_1")
    private String sensorr1;

    @Key("SENSOR_2")
    private String sensorr2;

    @Key("SENSOR_3")
    private String sensorr3;

    @Key("SENSOR_4")
    private String sensorr4;

    @Key("TEMP")
    private String temp;

    @Key("BAT")
    private String bat;


    public String getId() {
        return id;
    }



    public void setId(String id) {
        this.id = id;
    }



    public String getDatetime() {
        return datetime;
    }



    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }



    public String getMac() {
        return mac;
    }



    public void setMac(String mac) {
        this.mac = mac;
    }



    public String getSensorr1() {
        return sensorr1;
    }



    public void setSensorr1(String sensorr1) {
        this.sensorr1 = sensorr1;
    }



    public String getSensorr2() {
        return sensorr2;
    }



    public void setSensorr2(String sensorr2) {
        this.sensorr2 = sensorr2;
    }



    public String getSensorr3() {
        return sensorr3;
    }



    public void setSensorr3(String sensorr3) {
        this.sensorr3 = sensorr3;
    }



    public String getSensorr4() {
        return sensorr4;
    }



    public void setSensorr4(String sensorr4) {
        this.sensorr4 = sensorr4;
    }



    public String getTemp() {
        return temp;
    }



    public void setTemp(String temp) {
        this.temp = temp;
    }



    public String getBat() {
        return bat;
    }



    public void setBat(String bat) {
        this.bat = bat;
    }


    public Sensor() {
        // TODO Auto-generated constructor stub
    }
}

这里是我的在线数据库中的数据示例。

{"_id":"51a71254d06fdbee04000653","DATETIME":"2013-05-23T11:16:34Z","MAC":"0013a200407e0086","SENSOR_1":"91.19","SENSOR_2":"19.093333333333334","SENSOR_3":"439.3097042968143","SENSOR_4":"0.0","TEMP":"-17","BAT":"47","_acl":{"creator":"kid_VPuam4pfqf"},"_kmd":{"lmt":"2013-05-30T08:48:20.335Z","ect":"2013-05-30T08:48:20.335Z"}},{"_id":"51a71255d06fdbee04000654","DATETIME":"2013-05-23T11:17:33Z","MAC":"0013a200407e0086","SENSOR_1":"91.19","SENSOR_2":"19.093333333333334","SENSOR_3":"494.4776331634671","SENSOR_4":"0.0","TEMP":"-17","BAT":"47","_acl":

感谢您的帮助。

检查以下链接并修改您的代码,

1)Best way to compare dates in Android

2)Compare two dates in Java

3)http://www.mkyong.com/java/how-to-compare-dates-in-java/

检查下面的代码每 10 秒刷新一次数据,

首先为 Handler 声明一个全局变量来从 Thread 更新 UI 控件,如下所示

Handler mHandler = new Handler();

现在创建一个Thread,使用while循环,利用线程的sleep方法周期性的执行任务。

new Thread(new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(10000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            // Write your code here to update the UI.
                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
    }).start();

勾选这个Link

它有这样的例子

 Date.compareTo()
 Date.before(), Date.after() and Date.equals()
Calender.before(), Calender.after() and Calender.equals()

[http://www.mkyong.com/java/how-to-compare-dates-in-java/][1]