gpt4 book ai didi

android - Google Firebase 序列化/反序列化

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:11:46 26 4
gpt4 key购买 nike

我是 FIrebase 的新手,我有 2 个可能与之相关的问题。第一个是保存我的事件列表时。

//creating event
TVEvent tvEvent = new TVEvent(etTitle.getText().toString());
User host = ResourceManager.getUser();
String date = etDate.getText().toString();
String location = etLocation.getText().toString();
TVSet tvset = ResourceManager.getUser().getTvSets().get(0);
Event ev = new Event(tvEvent, host, date, location, tvset);
ResourceManager.addEvent(ev);
mDatabase.child("events").child(host.getId()).setValue(ResourceManager.getEvents()); //getEvents() returns a list of events

这是我在控制台中得到的 enter image description here

问题是 tvevent 和 tv set 的属性比这些多。当我调试以找出为什么 tvevent 是用它的所有属性创建的时,这有点奇怪。但是现在,我不知道这是否是个问题,因为我无法从数据库中检索 tvset 和 tvevent。当我执行以下操作时,我得到的是 null。

mDatabase.child("events").addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("Cheking for events");
//GenericTypeIndicator<List<Event>> t = new GenericTypeIndicator<List<Event>>() {};
//List<Event> e = dataSnapshot.getValue(t);
for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
GenericTypeIndicator<List<Event>> t = new GenericTypeIndicator<List<Event>>() {};
List<Event> list = messageSnapshot.getValue(t);
if (list != null) {
for (Event ev : list) {
System.out.println("Event found");
ResourceManager.addEvent(ev);
}
}
}


}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

我不知道为什么这些值为空,因为我可以看到它们不在 firebase 控制台中。那么问题是什么?

@IgnoreExtraProperties
public class Event {

private TVEvent tvEvent;
private User host;
private Date date;
private String location;
private TVSet tvSet;
private List<User> attending;

public Event(){
}

public Event(TVEvent tvEvent, User host, String date, String location, TVSet tvset){
this.tvSet = tvset;
this.tvEvent = tvEvent;
this.host = host;
try {
this.date = new SimpleDateFormat("dd/MM/yyyy").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}

this.location = location;

attending = new ArrayList<>();
}

public User getHost() {
return host;
}

public String getLocation() {
return location;
}

public TVSet getTVSet() {
return tvSet;
}

public Date getDate() {
return date;
}

public TVEvent getTVEvent() {
return tvEvent;
}

public void addAttending(User user){
attending.add(user);
}

public List<User> getAttending(){
return attending;
}
}

由于 TVSet 和 TVEvent 都没有正确反序列化,我将只发布其中一个:

@IgnoreExtraProperties
public class TVSet {

private String title;
private String imageFile;
//private Bitmap picture;

public TVSet(){

}

public TVSet(String title){
this.title = title;
}

public TVSet(Bitmap picture){
imageFile = compressImage(picture);
}

public void setTitle(String title){
this.title = title;
}

public String getTitle() {
return title;
}

private String compressImage(Bitmap image){
ByteArrayOutputStream bYtE = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, bYtE);
//image.recycle();
byte[] byteArray = bYtE.toByteArray();
String imageFile = Base64.encodeToString(byteArray, Base64.DEFAULT);
return imageFile;
}


@Exclude
public Bitmap getImage() {
byte[] decodedString = Base64.decode(imageFile, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return bitmap;
}

public String getImageFile(){
return imageFile;
}
}

抱歉格式错误。感谢您的帮助。

最佳答案

尝试使用

Map<String, String> data= (HashMap<String, String>)dataSnapshot.getValue();

代替

GenericTypeIndicator<List<Event>> t = new GenericTypeIndicator<List<Event>>() {};
List<Event> list = messageSnapshot.getValue(t);

关于android - Google Firebase 序列化/反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37978980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com