【问题标题】:Cannot add timestamp in Firebase?无法在 Firebase 中添加时间戳?
【发布时间】:2017-06-21 08:44:57
【问题描述】:

我在这里完成了 Firebase Android Codelab:https://codelabs.developers.google.com/codelabs/firebase-android/

发送消息时,消息的名称、照片网址和文本已组合在一起,如下所示:

我正在尝试在组中添加该消息的时间戳 (ServerValue.TIMESTAMP)。

应该发送消息的代码以及消息的名称、photoUrl 和文本 + 时间戳(最初来自 MainActivity.java):

    mSendButton = (Button) findViewById(R.id.sendButton);
    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername,
                    mPhotoUrl, ServerValue.TIMESTAMP); //It's not supposed to be like this! What should I write instead?
            mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage);
            mMessageEditText.setText("");
            mFirebaseAnalytics.logEvent(MESSAGE_SENT_EVENT, null);
        }
    });

使用上面的代码,我无法随意添加时间戳。为什么?我该怎么办?

FriendlyMessage.java:

    public class FriendlyMessage {

    private String id;
    private String text;
    private String name;
    private String photoUrl;
    private Long creationDate;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl, Long creationDate) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
        this.creationDate = creationDate;
    }

    public java.util.Map<String, String> getCreationDate() {
        return ServerValue.TIMESTAMP;
    }

    @Exclude
    public Long getCreationDateLong() {
        return creationDate;
    }

    public void setCreationDate(Long creationDate) {
        this.creationDate = creationDate;
    }

    public String getId() {
        return id;
    }

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

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
}

查看整个 Firebase Android Codelab 原始项目 here(无时间戳)。

【问题讨论】:

标签: java android firebase


【解决方案1】:

这样做

public class FriendlyMessage {

    private String id;
    private String text;
    private String name;
    private String photoUrl;
    private Long creationDate;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
    }

    public java.util.Map<String, String> getCreationDate() {
        return ServerValue.TIMESTAMP;
    }

    @Exclude
    public Long getCreationDateLong() {
        return creationDate;
    }

    public void setCreationDate(Long creationDate) {
        this.creationDate = creationDate;
    }

    public String getId() {
        return id;
    }

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

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();

        result.put("id", id);
        result.put("text", text);
        result.put("name", name);
        result.put("photoUrl", photoUrl);
        return result;

    }

}

在您的代码中的某处执行此操作

Date someDate = new Date();

FriendlyMessage friendlyMessage = new FriendlyMessage("bla","bla","bla");

 String key = mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().getKey();
 Map<String, Object> postValues =  friendlyMessage.toMap();
 postValues.put("creationDate", ServerValue.TIMESTAMP);
 childUpdates.put("/"+MESSAGES_CHILD+"/" + key, postValues);
 Map<String, Object> childUpdates = new HashMap<>();

 mFirebaseDatabaseReference.updateChildren(childUpdates);

【讨论】:

  • 这不是我想要的。看看我用你的代码得到了什么:imgur.com/NgaMoXc
  • 我想要一个真实的创作日期。不是'123' 你能修复你的代码吗?谢谢!
  • Hei Me123 你可以通过 someDate.getTime() 获取时间戳,我更新了代码 FriendlyMessagefriendlyMessage = new FriendlyMessage("bla","bla","bla",someDate.getTime() );
  • 但是,这不使用ServerValue.TIMESTAMP
  • 我要服务器收到消息的时间。
【解决方案2】:

您必须将 creationDate 变量的数据类型从 Long 更改为 Map。因为ServerValue.TIMESTAMP 返回一个 Map 值。

它应该是这样的:

public class FriendlyMessage {

    private String id;
    private String text;
    private String name;
    private String photoUrl;
    private Map creationDate;

    public FriendlyMessage() {
    }

    public FriendlyMessage(String text, String name, String photoUrl, Map CreationDate) {
        this.text = text;
        this.name = name;
        this.photoUrl = photoUrl;
        this.creationDate = creationDate;
    }

    public java.util.Map<String, String> getCreationDate() {
        return ServerValue.TIMESTAMP;
    }

    public void setCreationDate(Map creationDate) {
        this.creationDate = creationDate;
    }

    public String getId() {
        return id;
    }

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

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }
}

【讨论】:

  • 我确实对其进行了测试,并且可以正常工作。您可以将问题中的代码更改为 Map 后的代码吗?
  • 我认为我们有误会。你能发布你完整的测试代码吗?
  • 编辑了我的答案。看看吧
  • 好的。但是l,实际将消息推送到服务器的部分呢? (FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, mPhotoUrl, ServerValue.TIMESTAMP);
  • 那里没有任何变化
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 2016-12-26
相关资源
最近更新 更多