Retrieve ServerValue.Timestamp from Firebase in Android app, when data is sent(发送数据时,从 Android 应用中的 Firebase 检索 ServerValue.Timestamp)
问题描述
我想知道如何使用 Firebase 的 ServerValue.TIMESTAMP 方法,当我想在 Firebase 服务器上创建时间戳,然后将其检索到本地客户端.
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.
在 Firebase 指南中,只有 javascript 对此案例有更详细的描述,但我很难弄清楚如何将其转换为我的 Android 应用程序.
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.
提前致谢!
推荐答案
Firebase.ServerValue.TIMESTAMP 设置为 Map(包含 {.sv:"timestamp"}) 告诉 Firebase 用服务器的时间填充该字段.当该数据被读回时,它是实际的 unix 时间戳,它是一个 Long.
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.
这样的事情会起作用:
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);
再举一个例子,你可以看看我对这个问题的回答:Android 聊天在 DataSnapshot.getValue() 上崩溃
For another example, you can see my answer to this question: Android chat crashes on DataSnapshot.getValue() for timestamp
这篇关于发送数据时,从 Android 应用中的 Firebase 检索 ServerValue.Timestamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:发送数据时,从 Android 应用中的 Firebase 检索 ServerValue.Timestamp
基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 存储 20 位数字的数据类型 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
