有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java什么是com。火基。客户服务器值。响应时间戳?

我正在尝试在web上使用Firebase构建一个聊天应用程序,并创建一个Android客户端。 在我的web应用程序中,我将Firebase服务器的时间戳与消息一起发送,但我似乎在Android上遇到了一些问题。 使用Firebase的Android开源聊天应用程序,我使用了默认聊天。java类,但当我尝试使用ServerValue.TIMESTAMP发送时间戳时,我得到一个错误,因为我假设它将返回一个int,但它返回一个Map

目前,我正在使用一种变通方法,从设备本身获取时间,但我正试图保持一致性,并减少跨时区的用户使用该应用程序时出现错误的空间

这是我目前在这个街区的工作

private void sendMessage() {
    EditText inputText = (EditText)findViewById(R.id.messageInput);
    String input = inputText.getText().toString();
    Long timestamp = System.currentTimeMillis()/1000;
    if (!input.equals("")) {
        // Create our 'model', a Chat object
        Chat chat = new Chat(name, input, timestamp, userID);
        // Create a new, auto-generated child of that chat location, and save our chat data there
        ref.push().setValue(chat);
        inputText.setText("");
    }
}

聊天。爪哇

公共课堂聊天{

private String from;
private String text;
private int userID;
private Long timestamp;

// Required default constructor for Firebase object mapping
@SuppressWarnings("unused")
private Chat() { }

Chat(String from, String text, Long timestamp, int userID) {
    this.from = from;
    this.text = text;
    this.timestamp = timestamp;
    this.userID = userID;
}

public String getFrom() {
    return from;
}

public String getText() {
    return text;
}

public Long getTimestamp() {
    return timestamp;
}

public int getuserID() {
    return userID;
}

}


共 (1) 个答案

  1. # 1 楼答案

    评论太长了。我的意思更像这样,我在很多场合都用过

    var chatUserRef = new Firebase("your url to chat user");
    chatUserRef.child('time')
      .set(Firebase.ServerValue.TIMESTAMP)
      .on('value', function(snapshot){
         var current_server_time = snapshot.val();
      });
    

    它在javascript中,但在Java中原理应该是相同的