I am using the Retrofit (2.3.0) and I use the Retrofit-GSON-converter to create a POJO.
Let's say I have the following class:
public class Flight {
#SerializedName("fromCity")
private String fromCity;
#SerializedName("toCity")
private String toCity;
#SerializedName("fromAirportName")
private String fromAirportName;
#SerializedName("toAirportName")
private String toAirportName;
// Getters + Setters
}
Now when I use a retrofit call to fetch the model, it parses without error and I get a List with all values set but sometimes I get an empty string for fromCity and toCity from my server.
What I want is to set the value of fromCity and toCity to custom values once the object is constructed such that I do not iterate over the List of the objects and update them individually.
Related
I have an app that stores lots of data to work offline as well.
I have three classes, in a hierarchy like;
public class MainGroup
{
private UUID Oid;
private String name;
private Date CreatedOn;
}
-
public class Group
{
private UUID Oid;
private String name;
private Date CreatedOn;
private MainGruop MainGroup;
}
-
public class Product
{
private UUID Oid;
private String name;
private Date CreatedOn;
private MainGruop MainGroup;
private Group Group;
}
( Oid fields are selected as PrimaryKey with realm attribute. )
Let's say, all MainGroup objects were stored in Realm DB. Then, when i'm trying to insert Group objects, with nested MainGroup object but with only its Oid field to link its master, Realm updates the MainProduct record (with given Oid), and clear the other fields as nulls.
In same way, when i'm inserting Product objects and nested objects are includes only Oid, realm updates all fields with nulls.
So, there are more complex and deeply related objects and when i make a request to get JSON from server, i must produce a very big JSON response to keep data.
And mention to insert method; I'm creating java objects with JSON response via GSON and i'm using Realm.copyToRealmOrUpdate(obj); method to insert.
To reduce payload (JSON size, serialize and insertion process), i need to find a way to fix this issue.
Why is price null here?
JSON is retrieved from here (you could also use multiple parameters, that is why the below object uses Maps; example). Here is CoinGeckoCoinPrice:
public class CoinGeckoCoinPrice {
#Expose
private Map<String, Map<String, Double>> price;
public double getPrice(String slug, String pair) {
...
}
}
Here is my Retrofit2 call:
#GET("/api/v3/simple/price")
Call<CoinGeckoCoinPrice> getCoinPrice(#Query("ids") String coinSlugs, #Query("vs_currencies") String pairs);
I checked logcat for any GSON error message and there is none. So why would price be null?
It works if I change Retrofit2 call to use JsonObject:
#GET("/api/v3/simple/price")
Call<JsonObject> getCoinPrice(#Query("ids") String coinSlugs, #Query("vs_currencies") String pairs);
But I want to wrap the object so I can write data retrieval functions in the CoinGeckoCoinPrice class.
The Json you are getting back doesn't have a price property on it, so Gson doesn't know that it should be parsing the bitcoin object and its price in USD to that object. Given that your returned data can be for any key (i.e. bitcoin, ethereum...) and its properties can also have any keys (i.e. usd, euros...) you'll need to manually parse this by giving Gson a custom adapter to work with.
I am trying to insert a POJO to Firebase. However, some of the fields don't seem to be parsed into Firebase, but there is no warning or error.
I have this POJO:
public class Group {
public String name;
public String admin;
public List<String> addedUsers;
public List<String> invitedUsers;
public Group(String name, String admin, ArrayList<String> addedUsers, ArrayList<String> invitedUsers) {
this.name = name;
this.admin = admin;
this.addedUsers = addedUsers;
this.invitedUsers = invitedUsers;
}
public Group() {
// Default constructor required because we have a non-default constructor as well.
}
}
I upload to Firebase by doing so:
DatabaseReference groupRef = ref.child("Groups");
ArrayList<String> addedUsers = new ArrayList<String>();
addedUsers.add("email1#gmail.com");
addedUsers.add("email2#gmail.com");
ArrayList<String> invitedUsers = new ArrayList<String>();
Group newGroup = new Group("GroupName",
"email1#gmail.com", addedUsers, invitedUsers
);
groupRef.push().setValue(newGroup);
I end up with this object in Firebase:
I have a secondary issue now, I manually inserted the data into Firebase, but now I cannot map the Lists onto my Java Object, and are mapped as null, I know I am able to download the data fine;
I'm not sure what you mean that lists are not supported, as it seems that they are supported.
Basic write operations
For basic write operations, you can use setValue() to save data to a
specified reference, replacing any existing data at that path. You can
use this method to:
Pass types that correspond to the available JSON types as follows:
String
Long
Double
Boolean
Map<String, Object>
List<Object>
Pass a custom Java object, if the class that defines it has a default
constructor that takes no arguments and has public getters for the
properties to be assigned.
Firebase supports key value mapping. So lists are not supported. Change it to Map type, keep email addresses as key and assign a boolean value true or false.
My model is like:
public class MyModel{
private String mId;
private String mName;
T mAnObject;
}
How can I store T object in database.
If you need to save specific properties, just do that, otherwise you could serialize it to JSON, as an option (as demonstrated here).
Example:
Object myObj has three properties: String title, String subtitle, String text
1 - You can save them as separate database rows and then when you read from the database you recombine them to create your Object.
2 - You can also use Json to save the properties, this way you only need to store a single String in the database and when you read from the database convert the Json String to your Object.
I have an object
public class ArticleList extends RealmObject {
#PrimaryKey
private String id;
private String title;
private String subtitle;
private String image;
private String category;
}
What I want to do is to fetch result from Realm and them convert result to ArticleList[]
Fetch I do by using
RealmResults<ArticleList> results = realm.where(ArticleList.class).equalTo("category", "CategoryName").findAll();
What do I have to do next to get an array of objects ?
Simplest to convert into java ArrayList:
ArrayList<People> list = new ArrayList(mRealm.where(People.class).findAll());
List<ArticleList> unmanagedList = realm.copyFromRealm(results);
Will do it.
RealmResults has a toArray() method - also toArray(T[] contents) (note the RealmResults inheritance chain). You can use these as follows:
ArticleList[] resultArray = (ArticleList[]) results.toArray();
Or
ArticleList[] resultArray = results.toArray(new ArticleList[results.size()]);
Ideally, you'd want to use RealmResults instead. This allows you to get "free" updates to your data, as well as all the conveniences of a List.
Instead of trying to convert to an array, you should extend the abstract RealmBaseAdapter class from https://github.com/realm/realm-android-adapters to keep your results in sync.
Realm provides these classes as an example of how to create an auto-updating list with a RecyclerView or a ListView.