Android chat crashes on DataSnapshot.getValue() for timestamp

前端 未结 2 452
自闭症患者
自闭症患者 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:04

    I know this thread is quite old, but for anyone trying to implement a similar timestamp now, instead of using @JsonIgnore from the Jackson annotations, you can use the Firebase native @Exclude now.

    0 讨论(0)
  • 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<String, String> 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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题