I'm using retrofit to parse json, and after parsing it will give pojo objects. I want store data to sqlite so I'm extracting data from pojo to store it in db. Is this a good approach?
Because retrofit converts json to pojo and again I have to extract pojo to get data. This is time consuming; any better approach?
It is good approach as it keeps your data organised. The only part I didn't understand is what do you mean by extacting from pojo.
For insert or update operation you should use pojo instead of directly using json object or string.
Converting json to pojo keeps data ready for showing on UI as well as Storing in database without complex code.
For time managment I would suggest check out Suger ORM it will save you some time.
Hope it was helpful.
Related
If there is a Rest-API where I get a very large JSON but I only need a few fields of it, for example only the first block in it, do I need to manually create kotlin data classes for ALL the fields in the JSON to be able to parse work with json data? What's the current best practice of handling such a case?
I'm receiving json from an API with article objects which include an array of images for that article. Take a look.
I'm using retrofit with gson to convert the data to Java objects.
These objects extend RealmObject, so they can be saved to realm immediately.
Article
Image
I use copyToRealmOrUpdate for saving the received objects to realm.
The articles are perfectly stored, but the images aren't although they are in the returned retrofit objects.
It seems like they aren't persisted automatically.
Do I need to loop over those articles and then copyToRealmOrUpdate the images array?
Then simply call setImages() with the images RealmList?
What's the best way to solve this problem?
Let me know if you need more information. I'm currently on my mobile and could give you later more.
The problem seems to be that in the JSON string "images" is not an array of image objects, but it's a "data" object which itself is in turn an array of image objects.
Do you have control over the JSON format or is it just something you receive and have no control over?
Can we convert json format data coming from server into mysql database or means can we store
the data coming through json into mysql database.
Thanks .
Although not a 'yes' in the sense that you can just shove a JSON string at MySQL and have it insert the data itself, yes, you can programmatically take a JSON string, parse it, and insert that data into a MySQL database. You may want to look at something like JSON-to-MySQL, which is a set of PHP scripts that can take arbitrary JSON data and convert it to SQL data.
The most common way is parser you Json data to Object and store it in database.
I recommend you use Jackson, see here and here a simple example.
Hope its help
Hi I am newbie in android . I am retrieving data using json parsin and i want to insert the data in my database.So suggest me whether i should in xml/json form or directly in sqlite. I am good at sqlite but i am dont know to insert in json format please suggest me to insert in to Json Object.
The most useful solution is such a case will be to use libraries like GSON or Jackson for converting JSON response to POJO (btw, you can use nice site http://www.jsonschema2pojo.org/, which will help you to create the needed POJO automatically from your response). After that you can use ORM helpers to link these objects with SQLite database, like greenDAO or ORMLite. All these libs are well-documented, so you will dive inside them easily.
I'm very new to Android and I'm currently working on Android app that will parse JSON from a Restful API and display some of the data in a list view. I've looked into using GSON for parsing as the JSON was quite complex. Now my main problem is figuring out how to store the data somewhere so the app doesn't need to reload itself every time the activity is clicked on. I've looked at few questions here but they all seem to point to JSON only not GSON. Could anyone recommend an efficient way of doing this?
Thanks
if you want to store the data permanent it's the best way to insert your deserialized objects into a sqlite database or store them local to the disk as a file.
if your intention is to load the data for each startup, it's easier. just put your object into a public static object. so you can check if the object is not null or not. if it's null you have to load the data.
another opportunity is using the SharedPreferences.