Google Firebase序列化/反序列化 [英] Google Firebase serialize/deserialize

查看:141
本文介绍了Google Firebase序列化/反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是FIrebase的新手,我有两个问题可能会被连接。第一个是保存事件列表。

  //创建事件
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()返回事件列表

这就是我在控制台



问题是tvevent和tv集比这些属性有更多的属性。当我调试了解为什么tvevent是创建的所有属性有点奇怪。但现在,我不知道这是否是一个问题,因为我无法从数据库中检索tvset和tvevent。当我做下面的事情时,我得到了空。

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






@Override
public void onCancelled(DatabaseError databaseError){

}
});

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

pre $ @ $ c $ @ $ @ $ $ $ $ $ $ $ $ $

私人TVEvent tvEvent;
私人用户主机;
私人日期日期;
私人字符串位置;
private TVSet tvSet;
私人列表<使用者>主治;
$ b public Event(){
}
$ b $ 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(){
返回参加;






$因为TVSet和TVEvent都没有得到正确的反序列化将只发布其中的一个:

  @IgnoreExtraProperties 
public class TVSet {

private字符串标题;
private String imageFile;
//私人位图图片;

public TVSet(){

}

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

public TVSet(位图图片){
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);
返回imageFile;


$ b @Exclude
public Bitmap getImage(){
byte [] encodedString = Base64.decode(imageFile,Base64.DEFAULT);
位图位图= BitmapFactory.decodeByteArray(decodedString,0,decodedString.length);
返回位图;


public String getImageFile(){
return imageFile;


抱歉格式不正确。任何帮助表示赞赏。

解决方案

尝试使用



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



而不是

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


I am new to FIrebase and I have 2 problems with it that might be connected. First one is when saving my list of events.

//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

this is what I get in the console

The problem is that tvevent and tv set have more attributes than these ones. When I debugged to find out why is that tvevent was created with all of its attributes which was a little strange. For now, however I don't know whether this is a problem as I cannot retrieve the tvset and tvevent from the database. When i do the following I get the to be 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) {

        }
    });

I don't know why these values are null as I can see that they are not in the firebase console. So what is the problem?

@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;
}
}

As both TVSet and TVEvent does not get deserialised properly I will post only one of them:

@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;
}
}

Sorry for the bad formatting. Any help is appreciated.

解决方案

try to use

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

instead of

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

这篇关于Google Firebase序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆