我正在尝试使用Web上的Firebase构建聊天应用程序,并创建和Android客户端。在我的Web应用程序中,我将Firebase Server的时间戳与消息一起发送,但是在Android上似乎有点麻烦。使用适用于Android的Firebase的Opensource聊天应用程序时,我使用了默认的Chat.java类,但是当我尝试使用ServerValue.TIMESTAMP发送时间戳时,出现了错误,因为我认为它将返回int但返回了。现在,我正在使用一种解决方法,并从设备本身获取时间,但是如果跨时区的人们将要使用该应用程序,则我将尝试保持一致并减少错误的余地。这是我目前有关街区的工作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(""); }}和Chat.java公开课聊天{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;}} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 评论太久了。我的意思是更多类似的东西,我曾多次使用。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相同。 (adsbygoogle = window.adsbygoogle || []).push({});
10-07 19:15
查看更多