Storing a list of enums in firebase - android

I have the following code:
public class TestClass {
public ArrayList<ObjectTypes> list = new ArrayList<>();
public TestClass(){
list.add(ObjectTypes.type1);
}
}
public enum ObjectTypes {
type1,
type2,
type3,
type4,
}
fb.child("Test").setValue(new TestClass());
where fb is a DatabaseReference.
When running the code the application crashes and the following error appears:
com.google.firebase.database.DatabaseException: No properties to
serialize found on class
ObjectTypes
This problem did not appear in the old Firebase.

The problem is currently solved by enumerating the enum elements (enumerating an enum...)
In the above example:
public enum ObjectTypes {
type1(0),
type2(1),
type3(2),
type4(3),
}
Hopefully an easier less boiler plated way will be added in the future?

Related

deserialize complicated gson string

Hi I hope someone here can help me, I am working in an android app, I already serialize the following gson object
Screenshot:
the jsonobject has many subclasses like:
PreferencialaboraEstudio,Preferencialaboralarea, and more classes,
I transfer this gson object from an activity to a new activity, in order to deserialize this object I have implemented the following code in the new activity:
Intent intent = getIntent();
String Postulado = intent.getStringExtra("Postulado");//Postulado from extra is actually a gson object
Candidato candidato = gson.fromJson(Postulado, Candidato.class);
CandidatoPreferenciaLaboralEstado preflaboraledo = gson.fromJson(Postulado, CandidatoPreferenciaLaboralEstado.class);
I have the problem in CandidatoPreferenciaLaboralEstado, as you can see in the picture there are two items of this type class in the gson object, but my code only returns the first item and not the second one, is there a way to get all the items of this type "CandidatoPreferenciaLaboralEstado" from the gson?
Thank you very much for your time and assistance in this matter.
Not sure how your root model is, but you can have something like this:
public class Postulado {
private CandidatoPreferenciaLaboralEstado candidatoPreferenciaLaboralEstado;
private CandidatoSoftware candidatoSoftware;
public class CandidatoPreferenciaLaboralEstado {
private List<CandidatoPrefAttributes> candidatoAttributesList;
public class CandidatoPrefAttributes {
private Integer cveCandidato;
private Integer cveCandidatoPreferenciaLaboralEstado;
//More
}
}
public class CandidatoSoftware {
private List<CandidatoSoftwareAttributes> candidatoAttributesList;
public class CandidatoSoftwareAttributes {
private Integer cveCandidato;
private Integer cveCandidatoSoftware;
//More
}
}
}
With respective getters.
Also looks that the candidates (CandidatoPreferenciaLaboralEstado and CandidatoSoftware) and Candidate Preferences are very similar, maybe you can unify that models to one (Candidate and CandidatePrefferences) and use multiple serialized names like:
#SerializedName(value="candidatoPreferenciaLaboralEstado", alternate={"candidatoSoftware"})
Hope this can help you!

AutoValue: ClassCastException: LinkedTreeMap cannot be cast to

First of all, i'm using retrofit with gson and autovalue-gson extension
Check here : https://github.com/rharter/auto-value-gson
In API i have a different models, where API returns json with id, description etc and also array of messages, but these messages may vary depending on what type of social media i want to (facebook, twitter, instagram etc...). So i made model like this :
#AutoValue
public abstract class SocialStream<T> {
public abstract int id();
......
......
public abstract List<T> messages();
public static <T> TypeAdapter<SocialStream<T>> typeAdapter(Gson gson, TypeToken<? extends SocialStream<T>> typeToken) {
return new AutoValue_SocialStream.GsonTypeAdapter(gson, typeToken);
}
}
As you can see i also passed typetoken to AutoValue typeAdapter, and everything seems to work, i already logged whole json, but problem occurs when i try to use it as my model.
SocialStream<FacebookPost> post = getList().get(0);
method getList() returns array of these messages and this is the line where i'm getting exception:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.model.FacebookPost
at
I have exactly the same problem like me. The key is creating a customized deserializer as Gson has no idea about the real class inside.
try this
https://stackoverflow.com/a/34685986/2458190

Android Firebase - How to push Child object which extend Abstract class [duplicate]

I'm using the new firebase sdk for android and use the real database feature. When i use the getValue(simple.class) everything is fine. But when i want to parse a class which is a subclass, all the attribute of the mother class are null, and i have this type of error:
No setter/field for name found on class uk.edume.edumeapp.TestChild
public class TestChild extends TestMother {
private String childAttribute;
public String getChildAttribute() {
return childAttribute;
}
}
public class TestMother {
protected String motherAttribute;
protected String getMotherAttribute() {
return motherAttribute;
}
}
this function
snapshot.getValue(TestChild.class);
motherAttribute attribute is null, and I get
No setter/field for motherAttribute found on class uk.edume.edumeapp.TestChild
the Json that i parse is:
{
"childAttribute" : "attribute in child class",
"motherAttribute" : "attribute in mother class"
}
Firebaser here
This is a known bug in some versions of the Firebase Database SDK for Android: our serializer/deserializer only considers properties/fields on the declared class.
Serialization of inherited properties from the base class, is missing in the in releases 9.0 to 9.6 (iirc) of the Firebase Database SDK for Android. It was added back in versions since then.
Workaround
In the meantime you can use Jackson (which the Firebase 2.x SDKs used under the hood) to make the inheritance model work.
Update: here's a snippet of how you can read from JSON into your TestChild:
public class TestParent {
protected String parentAttribute;
public String getParentAttribute() {
return parentAttribute;
}
}
public class TestChild extends TestParent {
private String childAttribute;
public String getChildAttribute() {
return childAttribute;
}
}
You'll note that I made getParentAttribute() public, because only public fields/getters are considered. With that change, this JSON:
{
"childAttribute" : "child",
"parentAttribute" : "parent"
}
Becomes readable with:
ObjectMapper mapper = new ObjectMapper();
GenericTypeIndicator<Map<String,Object>> indicator = new GenericTypeIndicator<Map<String, Object>>() {};
TestChild value = mapper.convertValue(dataSnapshot.getValue(indicator), TestChild.class);
The GenericTypeIndicator is a bit weird, but luckily it's a magic incantation that can be copy/pasted.
This was apparently finally fixed in release 9.6.
Fixed an issue where passing a derived class to DatabaseReference#setValue() did not correctly save the properties from the superclass.
for:
No setter/field for motherAttribute found on class uk.edume.edumeapp.TestChild
put setter for TestChild class:
public class TestMother {
private String motherAttribute;
public String getMotherAttribute() {
return motherAttribute;
}
//set
public void setMotherAttribute(String motherAttribute) {
this.motherAttribute= motherAttribute;
}
}
Check this https://firebase.google.com/support/guides/firebase-android
it says
"If there is an extra property in your JSON that is not in your Java class, you will see this warning in the log files: W/ClassMapper: No setter/field for ignoreThisProperty found on class com.firebase.migrationguide.ChatMessage
"
Blockquote
You can get rid of this warning by putting an #IgnoreExtraProperties annotation on your class. If you want Firebase Database to behave as it did in the 2.x SDK and throw an exception if there are unknown properties, you can put a #ThrowOnExtraProperties annotation on your class.
Blockquote

Which one was the best approach to implementing Asynchronous service call in android

I would like to implement a service(Web service) call in my application.
I blocked here for a while.
Previously I followed some of the below concepts.
like, AsyncTask class, Thread concepts and Handlers.
Recently I heard about the Retrofit.
Based on by experience, retrofit was good with high performance.
But it's not that much of reliable.
Example:
{
"Tag1":"Tag Value",
"TagArray":[ {"key1":"value","key2":"value"},{"key1":"value","key2":"value"},{"key1":"value","key2":"value"} ]
}
POJO:
public class Data{
String key1,key2;
sterres...
getters..
}
If the response have some other tags that are no need and the inside data only we need in our app i,e. "TagArray".
I need the handle only this response.
In such type of case this retrofit was failed.
Is there any other libraries or any other components to implement service calls in android with high performance are existed.
You can add your POJOs' or Beans' fields #Optional. This option comes from GSON that used in Retrofit as default.
EDIT :
public class ExamplePojo implements Serializable {
#SerializedName("TagArray")
public ArrayList<Keys> TagArray;
public ExamplePojo() {
TagArray = new ArrayList<>();
}
public static class Keys{
#SerializedName("key1")
public String key1;
#SerializedName("key2")
public String key2;
}
}
The attributes which you ignore shouldnt be added on POJO class or as mention above, add optional annotation if it can be null.

Usage of parceler (#Parcel) with Realm.io (Android)

I have the following code which produces the error: Error:Parceler: Unable to find read/write generator for type io.realm.Realm for io.realm.RealmObject.realm
It was working all fine without extends RealmObject , however I want to use Realm to put to database easily. Is there a way to exlcude the RealmObject fields and just use the basic pojo fields for #Parcel?
#Parcel
public class Feed extends RealmObject{
int id;
public String text;
public String time_created;
String time_modified;
int comments_count;
int likes_count;
String feed_type;
int obj_id;
String image;
String user_name;
String user_earthmile_points;
boolean liked;
boolean commented;
boolean is_private;
String url;
int feed_creator_id;
}
EDIT #2: Actually, I found a way to make it work :). See the updated answer below.
EDIT #1: While the app compiles just fine, it crashes when trying to actually create a Parcel with the error: org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for io.realm.FeedRealmProxy. The Realm team has officially acknowledged that it is currently not possible to implement Parcelable on RealmObjects. It is unclear if / when this will be resolved.
With Parceler v0.2.16, you can do this:
#RealmClass // required if using JDK 1.6 (unrelated to Parceler issue)
#Parcel(value = Parcel.Serialization.BEAN, analyze = { Feed.class })
public class Feed extends RealmObject {
// ...
}
Then, use Parcels.wrap(Feed.class, feed) instead of Parcels.wrap(feed) everywhere, otherwise your app will crash with org.parceler.ParcelerRuntimeException: Unable to create ParcelableFactory for io.realm.FeedRealmProxy.
All classes that extend RealmObject will have a matching RealmProxy class created by the annotation processor. Parceler must be made aware of this class. Note that the class is not available until the project has been compiled at least once.
#Parcel(implementations = { PersonRealmProxy.class },
value = Parcel.Serialization.BEAN,
analyze = { Person.class })
public class Person extends RealmObject {
// ...}

Categories

Resources