Currently I have this model class to collect data from firebase database. To show the data I am using firebaserecycleradapter.
News.class
public class News {
private String message, author, thumb_author, type;
private Long timestamp;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getThumb_author() {
return thumb_author;
}
public void setThumb_author(String thumb_author) {
this.thumb_author = thumb_author;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
}
But the problem is there is another child named images that consist of pushId and an Object. This is how it looks
news {
message:"value"
author:"value"
thumb_author:"value"
type:"value"
timestamp:"value"
images {
pushId01:"value"
pushId02:"value"
pushId03:"value"
pushId04:"value"
pushId05:"value"
}
}
So my question is how can I add the image child to the news.class or is there any possible solution for this? My possible solution for this is to get the image child data inside the onBindViewHolder using databasereference.
Add a property to represent the images node. Since these are simple string key/value pairs, you can represent it with a Map<String,String>. So:
public class News {
private String message, author, thumb_author, type;
private Long timestamp;
public Map<String,String> images; // this is a new field for the images
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
...
For the below code, my nested parameterized object is always deserialized as LinkedTreeMap instead of the original class
I am using GSON for json serializing & deserializing
Here are the models:
Cart containing wrapped items
public class Cart {
private String id;
private String owner;
private List<ItemWrapper> relatedItems;
.......
public List<ItemWrapper> getRelatedItems() {
return relatedItems;
}
public void setRelatedItems(List<ItemWrapper> relatedItems) {
this.relatedItems = relatedItems;
}
}
Item wrapper
public class ItemWrapper<T> {
private String type;
private String decription;
private T item;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
........
public T getItem() {
return item;
}
public void setItem(T item) {
this.item = item;
}
}
Car Item ..
public class Car {
private String model;
private double price;
public String getModel() {
return model;
}
.....
public void setPrice(double price) {
this.price = price;
}
}
Book Item ..
public class Book {
private String name;
private String mediaType;
public String getName() {
return name;
}
.....
public void setMediaType(String mediaType) {
this.mediaType = mediaType;
}
}
When I run the below snippet
Cart cart = gson.fromJson(
"{\"id\":\"id123\",\"owner\":\"Usama\",\"relatedItems\":[{\"type\":\"Book\",\"decription\":\"book item\",\"item\":{\"name\":\"Love\",\"mediaType\":\"pdf\"}},{\"type\":\"Car\",\"decription\":\"car item\",\"item\":{\"model\":\"BMW\",\"price\":500000.0}}]}\n"
+ "",
Cart.class);
System.out.println(cart.getClass().getName());
System.out.println(cart.getRelatedItems().get(0).getItem().getClass().getName());
I got that result
model.Cart
com.google.gson.internal.LinkedTreeMap
instead of
model.Cart
model.Book
Any idea how to fix this.
Im trying to retrieve a Document but I just can't get it to work. Here is my Code:
fm.getColRefProgramms().document(programmKey).collection("items").document("HIXQobZtlMxn4OblgOMi").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
String s = documentSnapshot.getId();
OverviewItem item = documentSnapshot.toObject(OverviewItem.class);
item.setKey(item.getKey());
}
});
String s return the correct Id, so the Document is found and loaded from the Database but it canĀ“t create a OverviewIdem Object from it.
I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference
And this is the OverviewItem Class:
public class OverviewItem implements IProgrammItem, Serializable{
private String instance;
private String key;
private Programm programm;
private ArrayList<BasicElement> elements;
public OverviewItem() {}
public void setInstance(String instance) {
this.instance = instance;
}
public String getInstance() {
return instance;
}
#Exclude
public Programm getProgramm() {
return programm;
}
#Exclude
public String getKey() {
return key;
}
#Exclude
public void setKey(String key) {
this.key = key;
}
#Exclude
public void setProgramm(Programm programm) {
this.programm = programm;
}
public ArrayList<BasicElement> getElements() {
return elements;
}
public void setElements(ArrayList<BasicElement> elements) {
this.elements = elements;
}
}
And this is my Document:
FIX: I waited for half an hour, then it started to retrieve the data (Date's value was retrieved and wasn't null anymore). Maybe the database takes time to update, so give it some time (if you added a new value or document) and then check again.
I'm facing the same issue. This is my code:
firebasefirestore.collection("events").addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot documentSnapshots, #Nullable FirebaseFirestoreException e) {
for(DocumentChange doc: documentSnapshots.getDocumentChanges()){
if(doc.getType() == DocumentChange.Type.ADDED){
Map<String, Object> a = doc.getDocument().getData();
HomeViewModel event = doc.getDocument().toObject(HomeViewModel.class);
eventlist.add(event);
eventRecyclerAdapter.notifyDataSetChanged();
}
}
}
});
Here I created a variable 'a' to see the data, and the data is being successfully retrieved from Firestore (I was checking in the debugger).
Value of a
However when I use toObject(), Date is null.
My HomeViewModel class:
public class HomeViewModel{
private String Date;
private String Description;
private String Imagepath;
private String Title;
public void setDate(String date) {
this.Date = date;
}
public String getDate() {
return Date;
}
public String getImagepath() {
return Imagepath;
}
public void setImagepath(String imagepath) {
Imagepath = imagepath;
}
public void setDescription(String description) {
Description = description;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public String getTitle() {
return Title;
}
public HomeViewModel(){
}
public HomeViewModel(String title, String desc, String date, String imagepath) {
this.Title = title;
this.Description = desc;
this.Date = date;
this.Imagepath = imagepath;
}
}
Therefore it is clear that toObject is the problem.
My ArrayList is like the attached image.
So, I want to distinct the list and need to post the data to server.
So I have created the Serialized model class like
#SerializedName("VehicleList")
public List<VehicleList> vehicleList = new ArrayList<>();
public static class VehicleList {
#SerializedName("VehicleNumber")
public String vehicleNumber;
#SerializedName("Mileage")
public String mileage;
#SerializedName("Coupons")
public List<Coupons> coupons = new ArrayList<>();
}
public static class Coupons {
#SerializedName("Code")
public String code;
}
My ArrayList like this
List<CodeItem> mList = new ArrayList<CodeItem>();
code item look like this
public class CodeItem {
private String code;
private String status;
private String vehicleID;
private String mileage;
private String date;
public boolean selected;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getVehicleID() {
return vehicleID;
}
public void setVehicleID(String vehicleID) {
this.vehicleID = vehicleID;
}
public String getMileage() {
return mileage;
}
public void setMileage(String mileage) {
this.mileage = mileage;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
Now I want to distinct the data for vehicle id and post it to sever. So how do I distinct the data by VehicleID from this mList?
Thanks in advance
I am using Flickr API to get the information of images and returns the following JSON:
{"photos":{"page":1,"pages":60,"perpage":100,"total":"5964","photo":[{"id":"21577339501","owner":"85277110#N02","secret":"31e850dfeb","server":"5785","farm":6,"title":"P1390956","ispublic":1,"isfriend":0,"isfamily":0}, {"id":"21577287101","owner":"85277110#N02","secret":"412990658f","server":"611","farm":1,"title":"P1400012","ispublic":1,"isfriend":0,"isfamily":0}]
I use this code in the Spring controller to deserialize the JSON:
Collection<Photos> readValues = objectMapper.readValue(new URL(url), new TypeReference<Collection<Photos>>() { });
And returns the following error:
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
How can I solve this problem? I didn't found solutions.
Photos.class:
public class Photos {
#JsonProperty("page")
private Integer page;
#JsonProperty("pages")
private Integer pages;
#JsonProperty("perpage")
private Integer perpage;
#JsonProperty("total")
private Integer total;
#JsonProperty("photo")
#JsonDeserialize(contentAs = Photo.class, as = ArrayList.class)
private List<Photo> photo;
public Photos() {}
public Photos(Integer page, Integer pages, Integer perpage, Integer total,
List<Photo> photo) {
super();
this.page = page;
this.pages = pages;
this.perpage = perpage;
this.total = total;
this.photo = photo;
}
public Photos(List<Photo> photo) {
super();
this.photo = photo;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
public Integer getPerpage() {
return perpage;
}
public void setPerpage(Integer perpage) {
this.perpage = perpage;
}
public Integer getTotal() {
return total;
}
public void setTotal(Integer total) {
this.total = total;
}
public List<Photo> getPhoto() {
return photo;
}
public void setPhoto(List<Photo> photo) {
this.photo = photo;
}
}
Photo.class:
public class Photo {
#JsonProperty("id")
private Integer id;
#JsonProperty("owner")
private String owner;
#JsonProperty("secret")
private String secret;
#JsonProperty("server")
private Integer server;
#JsonProperty("farm")
private Integer farm;
#JsonProperty("title")
private String title;
#JsonProperty("ispublic")
private Boolean isPublic;
#JsonProperty("isfriend")
private Boolean isFriend;
#JsonProperty("isfamily")
private Boolean isFamily;
public Photo() { }
public Photo(Integer id, String owner, String secret, Integer server,
Integer farm, String title, Boolean isPublic, Boolean isFriend,
Boolean isFamily) {
super();
this.id = id;
this.owner = owner;
this.secret = secret;
this.server = server;
this.farm = farm;
this.title = title;
this.isPublic = isPublic;
this.isFriend = isFriend;
this.isFamily = isFamily;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
public Integer getServer() {
return server;
}
public void setServer(Integer server) {
this.server = server;
}
public Integer getFarm() {
return farm;
}
public void setFarm(Integer farm) {
this.farm = farm;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Boolean getIsPublic() {
return isPublic;
}
public void setIsPublic(Boolean isPublic) {
this.isPublic = isPublic;
}
public Boolean getIsFriend() {
return isFriend;
}
public void setIsFriend(Boolean isFriend) {
this.isFriend = isFriend;
}
public Boolean getIsFamily() {
return isFamily;
}
public void setIsFamily(Boolean isFamily) {
this.isFamily = isFamily;
}
}
The basic problem is that your json is not a Collection<Photos>, but a Map<String, Photos>, which has a single entry "photos" -> Photos instance.
I got your json to successfully deserialize by making the following changes...
A) Change the type being read:
Map<String, Photos> readValues = objectMapper.readValue(json, new TypeReference<Map<String, Photos>>() { });
Note that I read straight from a String (not a URL).
B) Change the type of Photo.id from Integer to Long, because your json has id values well exceeding max int size.
C) I added the missing two closing braces from your sample json to make it valid.
FYI, deserialization works with or without the #JsonDeserialize annotation on the List<Photo> photo field of Photos.
Here's some runnable code that works:
String json = "{\"photos\":{\"page\":1,\"pages\":60,\"perpage\":100,\"total\":\"5964\",\"photo\":[{\"id\":\"21577339501\",\"owner\":\"85277110#N02\",\"secret\":\"31e850dfeb\",\"server\":\"5785\",\"farm\":6,\"title\":\"P1390956\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0}, {\"id\":\"21577287101\",\"owner\":\"85277110#N02\",\"secret\":\"412990658f\",\"server\":\"611\",\"farm\":1,\"title\":\"P1400012\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0}]}}";
Map<String, Photos> readValues = new ObjectMapper().readValue(json, new TypeReference<Map<String, Photos>>() { });