Android chat crashes on DataSnapshot.getValue() for timestamp

前端 未结 2 566
自闭症患者
自闭症患者 2020-12-03 16:29

I\'m trying to modify Firebase\'s Android chat example to include Firebase timestamp values. I can send the timestamp just fine using ServerValue.TIMESTAMP; but

2条回答
  •  情书的邮戳
    2020-12-03 17:16

    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. When Firebase (using Jackson) tries to deserialize that returned value, it fails since it is not a Map.

    You could use Jackson annotations to control how your class is converted to JSON.

    For example, something like this should work:

    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.firebase.client.ServerValue;
    
    public class Thing {
        private String id;
        private Long creationDate;
    
        public Thing() {
    
        }
    
        public Thing(String id) {
            this.id = id;
        }
    
        public String getId() {
            return id;
        }
    
        public java.util.Map getCreationDate() {
            return ServerValue.TIMESTAMP;
        }
    
        @JsonIgnore
        public Long getCreationDateLong() {
           return creationDate;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public void setCreationDate(Long creationDate) {
            this.creationDate = creationDate;
        }
    }
    

提交回复
热议问题