Lyl*_*yla 23 android firebase firebase-realtime-database
当您创建一个简单的旧Java对象,该对象旨在从Firebase序列化和反序列化时,有没有办法使用该ServerValue.TIMESTAMP
值?
例如,假设我想要一个对象,其中一个属性是最后一次编辑,并且您想要使用该ServerValue.TIMESTAMP
值.
在POJO类中,您可能具有:
private String timeLastChanged;
Run Code Online (Sandbox Code Playgroud)
要么
private Map<String, String> timeLastChanged;
Run Code Online (Sandbox Code Playgroud)
在第一个例子中String
,我遇到了设置问题timeLastChange = ServerValue.TIMESTAMP;
,因为ServerValue.TIMESTAMP
是Map.
在第二个例子中,Map<String, String>
我得到了一个"未能去抖"的错误,因为它无法正确地将数据库中存储的long反序列化为一个Map<String, String>
.这有什么工作吗?
Lyl*_*yla 28
2016年12月27日更新
正如许多人提到的那样,为@Exclude切换了@JsonIgnore .
我终于想出了一个灵活的解决方案来处理Dates和ServerValue.TIMESTAMP.这是Ivan V,Ossama和puf的例子.
我无法想出一个办法来对付之间的转换long
和HashMap<String, String>
,但如果你窝在一个更通用的属性HashMap<String, Object>
就可以进入该数据库可作为单一的长值("日期","1443765561874")或为ServerValue.TIMESTAMP
哈希映射("日期",{".sv","servertime"}).然后当你把它拉出来时,它总是一个HashMap("date","some long number").然后,您可以使用@JsonIgnore @Exclude批注在POJO类中创建辅助方法(意味着Firebase将忽略它,而不将其视为用于序列化到数据库或从数据库序列化的方法),以便从返回的HashMap轻松获取long值.在您的应用中使用.
下面是POJO类的完整示例:
import com.google.firebase.database.Exclude;
import com.firebase.client.ServerValue;
import java.util.HashMap;
import java.util.Map;
public class ExampleObject {
private String name;
private String owner;
private HashMap<String, Object> dateCreated;
private HashMap<String, Object> dateLastChanged;
/**
* Required public constructor
*/
public ExampleObject() {
}
public ExampleObject(String name, String owner, HashMap<String,Object> dateCreated) {
this.name = name;
this.owner = owner;
this.dateCreated = dateCreated;
//Date last changed will always be set to ServerValue.TIMESTAMP
HashMap<String, Object> dateLastChangedObj = new HashMap<String, Object>();
dateLastChangedObj.put("date", ServerValue.TIMESTAMP);
this.dateLastChanged = dateLastChangedObj;
}
public String getName() {
return name;
}
public String getOwner() {
return owner;
}
public HashMap<String, Object> getDateLastChanged() {
return dateLastChanged;
}
public HashMap<String, Object> getDateCreated() {
//If there is a dateCreated object already, then return that
if (dateCreated != null) {
return dateCreated;
}
//Otherwise make a new object set to ServerValue.TIMESTAMP
HashMap<String, Object> dateCreatedObj = new HashMap<String, Object>();
dateCreatedObj.put("date", ServerValue.TIMESTAMP);
return dateCreatedObj;
}
// Use the method described in https://stackoverflow.com/questions/25500138/android-chat-crashes-on-datasnapshot-getvalue-for-timestamp/25512747#25512747
// to get the long values from the date object.
@Exclude
public long getDateLastChangedLong() {
return (long)dateLastChanged.get("date");
}
@Exclude
public long getDateCreatedLong() {
return (long)dateCreated.get("date");
}
}
Run Code Online (Sandbox Code Playgroud)
ser*_*y.n 18
我想稍微改善一下Lyla的答案.首先,我想摆脱公共方法public HashMap<String, Object> getDateLastChanged()
public HashMap<String, Object> getDateCreated()
.为此,您可以dateCreated
使用@JsonProperty
注释标记属性.另一种可能的方式这样做是要改变性质检测这样的:@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
第二,我不明白为什么我们需要把ServerValue.TIMESTAMP
进HashMap
,而我们只需将其保存为属性.所以我的最终代码是:
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.firebase.client.ServerValue;
public class ShoppingList {
private String listName;
private String owner;
@JsonProperty
private Object created;
public ShoppingList() {
}
public ShoppingList(String listName, String owner) {
this.listName = listName;
this.owner = owner;
this.created = ServerValue.TIMESTAMP;
}
public String getListName() {
return listName;
}
public String getOwner() {
return owner;
}
@JsonIgnore
public Long getCreatedTimestamp() {
if (created instanceof Long) {
return (Long) created;
}
else {
return null;
}
}
@Override
public String toString() {
return listName + " by " + owner;
}
}
Run Code Online (Sandbox Code Playgroud)
这些解决方案对我来说似乎有点困难,因为我不知道@JsonIgnore在做什么.我试图以一种简单的方式做到这一点,看起来很有效.
private Object dateLastChanged;
public Object getDateLastChanged() {
return dateLastChanged;
}
Run Code Online (Sandbox Code Playgroud)
为了获得人类可读的东西,我只需将返回值dateLastChanged Object转换为以下方法,方法是将其转换为Long.
static String convertTime(Long unixtime) {
Date dateObject = new Date(unixtime);
SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MM-yy hh:mmaa");
return dateFormatter.format(dateObject);
}
Run Code Online (Sandbox Code Playgroud)
欢迎就我的解决方案发表意见^^
小智 5
您可以使用Firebase本机,而不是使用@JsonIgnore @Exclude
.例如,我在一个类似的项目中工作,我的模型是这样的.
package com.limte.app.borrador_1.mModel;
import com.google.firebase.database.Exclude;
import com.google.firebase.database.ServerValue;
/**
* Created by Familia on 20/12/2016.
*/
public class ChatItem {
String chatName;
Long creationDate;
public ChatItem() {
}
public String getChatName() {
return chatName;
}
public void setChatName(String chatName) {
this.chatName = chatName;
}
public java.util.Map<String, String> getCreationDate() {
return ServerValue.TIMESTAMP;
}
@Exclude
public Long getCreationDateLong() {
return creationDate;
}
public void setCreationDate(Long creationDate) {
this.creationDate = creationDate;
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码有效!在Firebase中,结果是: 结果
归档时间: |
|
查看次数: |
11487 次 |
最近记录: |