Android Firebase数据库调用TargetException
嗨,我在尝试将对象写入Firebase数据库时遇到此错误,错误如下: ` 当我试图从Firebase添加ServerValue.TIMESTAMP时,错误开始了,受Udacity示例的指导,下面是我的POJO:Android Firebase数据库调用TargetException,android,database,firebase,firebase-realtime-database,Android,Database,Firebase,Firebase Realtime Database,嗨,我在尝试将对象写入Firebase数据库时遇到此错误,错误如下: ` 当我试图从Firebase添加ServerValue.TIMESTAMP时,错误开始了,受Udacity示例的指导,下面是我的POJO: public class Messages { private String content, author, authorPhoto; private int type; protected HashMap<String, Object> time
public class Messages {
private String content, author, authorPhoto;
private int type;
protected HashMap<String, Object> timeStampCreated;
private Uri uri;
public Messages() {//Empty default constructor, necessary for FireBase to be able to deserialize blog posts
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public String getAuthor() {
return author;
}
public String getAuthorPhoto() {
return authorPhoto;
}
public int getType() {
return type;
}
@JsonIgnore
public long getTimeStamp() {
return (long)timeStampCreated.get("timeStamp");
}
public void setTimeStamp() {
HashMap<String, Object> hmDate = new HashMap<>();
hmDate.put("timeStamp", ServerValue.TIMESTAMP);
this.timeStampCreated = hmDate;
}
public void setAuthor(String author) {
this.author = author;
}
public void setAuthorPhoto(String authorPhoto) {
this.authorPhoto = authorPhoto;
}
public void setType(int type) {
this.type = type;
}
public Uri getUri() {
return uri;
}
public void setUri(Uri uri) {
this.uri = uri;
}
}
请帮忙 Uri不是受支持的数据类型,请参阅
尝试将其替换为字符串。Uri不是受支持的数据类型,请参阅
尝试将其替换为字符串。我对此进行了一段时间的实验,得出结论是不可能按照您希望的方式在POJO中使用
ServerValue.TIMESTAMP
您可能知道,ServerValue.TIMESTAMP
是一个映射。在非POJO用例中,您可以将其作为setValue()
的Object
参数提供,或者作为setValue()
的映射的元素提供。处理数据存储请求时,Firebase服务器识别特殊标记值,并用服务器时间替换它。随后读取数据时,数据将以长
格式返回
序列化的POJO处理基于具有固定类型的字段。我不知道如何才能使它适用于需要在输出时为映射
,在输入时为长
的字段
如果您可以容忍较不准确的时间戳,您可以考虑添加监听器来监视时钟偏移,并从中派生出自己的时间戳。这在中有描述。
我对此进行了一段时间的实验,并得出结论,在POJO中不可能以您想要的方式使用ServerValue.TIMESTAMP
您可能知道,ServerValue.TIMESTAMP
是一个映射。在非POJO用例中,您可以将其作为setValue()
的Object
参数提供,或者作为setValue()
的映射的元素提供。处理数据存储请求时,Firebase服务器识别特殊标记值,并用服务器时间替换它。随后读取数据时,数据将以长
格式返回
序列化的POJO处理基于具有固定类型的字段。我不知道如何才能使它适用于需要在输出时为映射
,在输入时为长
的字段
如果您可以容忍较不准确的时间戳,您可以考虑添加监听器来监视时钟偏移,并从中派生出自己的时间戳。这在中有描述。
我修复了,但我没有工作,我在这一行添加了一个断点:FBRefMessages.push().setValue(message,new DatabaseReference.CompletionListener()),我不知道为什么,但是引用设置为这个值:没有这样的实例字段:“FBRefMessages”我尝试用JsonIgnore
删除getTimeStamp
,它成功了,但我需要添加ServerValue.TIMESTAMP
,我如何添加它?我修复了这个问题,但没有成功,我在这行添加了一个断点:FBRefMessages.push().setValue(消息,新数据库引用.CompletionListener()),我不知道为什么,但是引用设置为这个值:没有这样的实例字段:“FBRefMessages”我尝试用JsonIgnore
删除getTimeStamp
,它成功了,但我需要添加ServerValue.TIMESTAMP
,我如何添加它?因此没有任何解决方法可以帮助我使用ServerValue、 时间戳
?也许你会从比我更聪明的人那里得到另一个答案。我看不到在POJO中使用它的方法。另一种方法是将创建时间存储为一个单独的值,在消息
POJO之外。如果调用setValue()如果将时间戳组合在一起,则时间戳应精确到一秒钟以内。因此,没有任何解决方法可以帮助我使用服务器值。时间戳
?也许你会从比我更聪明的人那里得到另一个答案。我看不到一种方法可以按照你想要的方式在POJO中使用它。另一种方法是将创建时间存储为单独的value,在您的消息
POJO之外。如果同时调用setValue(),则时间戳应精确到一秒钟之内。
public class Messages {
private String content, author, authorPhoto;
private int type;
protected HashMap<String, Object> timeStampCreated;
private Uri uri;
public Messages() {//Empty default constructor, necessary for FireBase to be able to deserialize blog posts
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public String getAuthor() {
return author;
}
public String getAuthorPhoto() {
return authorPhoto;
}
public int getType() {
return type;
}
@JsonIgnore
public long getTimeStamp() {
return (long)timeStampCreated.get("timeStamp");
}
public void setTimeStamp() {
HashMap<String, Object> hmDate = new HashMap<>();
hmDate.put("timeStamp", ServerValue.TIMESTAMP);
this.timeStampCreated = hmDate;
}
public void setAuthor(String author) {
this.author = author;
}
public void setAuthorPhoto(String authorPhoto) {
this.authorPhoto = authorPhoto;
}
public void setType(int type) {
this.type = type;
}
public Uri getUri() {
return uri;
}
public void setUri(Uri uri) {
this.uri = uri;
}
}
public void sendMessage(String content, final int type) {
Messages message = new Messages();
message.setContent(content);
message.setTimeStamp();
message.setAuthor(email);
message.setAuthorPhoto(picURL);
message.setType(type);
FBRefMessages.push().setValue(message, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError error, DatabaseReference databaseReference) {
if (error == null) {
Log.d("Adapter", "onChildAdded: The adapter is: " + mAdapter);
updateNodes();
} else {//There was an error
}
}
});
}