在发送数据时从Android应用的Firebase中检索ServerValue.Timestamp [英] Retrieve ServerValue.Timestamp from Firebase in Android app, when data is sent

查看:275
本文介绍了在发送数据时从Android应用的Firebase中检索ServerValue.Timestamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Firebase的ServerValue.TIMESTAMP方法,当我想在Firebase服务器上创建一个时间戳,然后将其检索到本地客户端。



在Firebase指南中,只有javascript对这种情况有更详细的描述,但是我很难理解如何将其转化为我的Android应用程序。



在此先感谢!

解决方案

Firebase.ServerValue.TIMESTAMP is设置为 Map (包含 {。sv:timestamp} ),它告诉Firebase使用服务器的时间。当这些数据被读回时,它是一个实际的unix时间戳,它是一个 Long



将工作:

  Firebase ref = new Firebase(https://YOUR-FIREBASE.firebaseio.com); 

ref.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
Long timestamp =(Long)snapshot.getValue() ;
System.out.println(timestamp);
}
$ b $ @Override
public void onCancelled(DatabaseError databaseError){

}
});

ref.setValue(ServerValue.TIMESTAMP);

另外一个例子,你可以看到我对这个问题的回答: Android的聊天崩溃DataSnapshot.getValue()的时间戳


I would like to know how to use Firebase's ServerValue.TIMESTAMP method, when I want to create a timestamp at the Firebase server, and then retrieve it to the local client.

In Firebase guides, only javascript has a more detailed description of this case, but I'm having a hard time figureing how to translate this in to my Android appliction.

Thanks in advance!

解决方案

Firebase.ServerValue.TIMESTAMP is set as a Map (containing {.sv: "timestamp"}) which tells Firebase to populate that field with the server's time. When that data is read back, it is the actual unix time stamp which is a Long.

Something like this will work:

Firebase ref = new Firebase("https://YOUR-FIREBASE.firebaseio.com");    

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        Long timestamp = (Long) snapshot.getValue();
        System.out.println(timestamp);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

ref.setValue(ServerValue.TIMESTAMP);

For another example, you can see my answer to this question: Android chat crashes on DataSnapshot.getValue() for timestamp

这篇关于在发送数据时从Android应用的Firebase中检索ServerValue.Timestamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆