Google Firebase serializar / deserializar
Soy nuevo en FIrebase y tengo 2 problemas con él que podrían estar conectados. La primera es cuando guardo mi lista de eventos.
//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
Esto es lo que obtengo en la consola
- La aplicación de Android no recibe la notificación de FCM cuando se cierra
- Excluir el dispositivo de prueba del registro de Firebase Analytics
- FirebaseApp con nombre no existe obtener error
- Firebase Cloud Messaging - ¿Cómo validar Tokens?
- El proyecto Android de Firebase Cloud Messaging no envía notificaciones push
El problema es que tvevent y tv tienen más atributos que estos. Cuando depuse para averiguar por qué es que tvevent fue creado con todos sus atributos que era un poco extraño. Por ahora, sin embargo no sé si esto es un problema ya que no puedo recuperar el tvset y tvevent de la base de datos. Cuando hago lo siguiente consigo el para ser nulo.
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) { } });
No sé por qué estos valores son nulos, ya que puedo ver que no están en la consola firebase. Entonces, ¿cuál es el problema?
@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; } }
Como TVSet y TVEvent no se deserializan correctamente, publicaré solo uno de ellos:
@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; } }
Lo siento por el mal formato. Cualquier ayuda es apreciada.
- Los datos extraídos de FirebaseDatabase se muestran en 3 AlertDialogs separados en lugar de uno
- Descripción de la función createUser de firebase (específicamente la biblioteca android)
- Cross-plataforma de texto, imagen, video Chatear para Android, iOS y Web utilizando Firebase / Parse / PubNub
- Clase de descriptor de módulo local para com.google.firebase.auth no encontrada
- Android - Firebase jobdispatcher
- UserProfileChangeRequest setPhotoUri - ¿Cómo se hace?
- Firebase sobrescribe Signin con la cuenta de Google
- Vista de búsqueda de Android en los datos de Firebase
tratar de usar
Map<String, String> data= (HashMap<String, String>)dataSnapshot.getValue();
en lugar de
GenericTypeIndicator<List<Event>> t = new GenericTypeIndicator<List<Event>>() {}; List<Event> list = messageSnapshot.getValue(t);