To get Data for my application, I parse a Json file, with Jackson, to (lists of) custom Objects. When I start my app, I check if there is a new Json file available and ask the user if they want to download it, else I use the "old" Json file. But every time I start my app I parse the Json. Then I use the Application Class to save my list of objects an go to my data when I want, most of the time I only need one object.
From the huge list, with multiple layer nested object, I create a simple "flat" arraylist of custom objects in which I put only the data I need to create listviews (name, id, second text and url of picture). When something is clicked, i use the id to get all the data.
Parsing this whole Json file every time is pretty time consuming and makes the startup time of my application long. Ofcourse, this sucks.
And having this huge list of custom objects saved in Application Class fills a lot of memory of my device, and sometimes after some use the class gets killed, and I need to reparse again.
Is there a way I don't need to reparse all my data?
I hoped for a process like this:
new Json file
first time parse total JSON to list of multilayered custom objects
create simple list for listviews
delete/clear the big list
some clever way to get only one of the giant items, without keeping the whole list in my memory. (maybe something with Jackson).
on destroying of the application maybe save the simple list, i read something about parceable or serializable?
Anyone knows how to achieve this?
Or has an other awesome idea?
Jackson has a streaming api. Also you can parse the json in a AsyncTask (in the background) and update your user interface once the new data is ready
I'd probably store the data in a SQLite database, in line with how the Android platform was designed.
As an alternative to streaming Jackson API (which is very fast, but still has to scan through most of the content), perhaps you could just save things in different files, one per entry? Or, if there is a way to group things, in multiple files each having some subset?
Of course, if you really have tons of entries, use of SQLite as Bruce suggested makes lots of sense.
Related
I'm building an Android app, which should:
show some data, loaded from a server in the Internet.
At the moment I have a local SQliteDB used in my app where the data is stored, which should be displayed. I use this, because I want to be able to show the data, even if there is temporarily no internet connection available.
Next step I will work on inserting data in the local SQliteDB from a internet server. I thought about doing it this way:
When app starts, check if internet is available. If yes, connect to a webservice (including username and password). The webservice should deliver the necessary data via json object to the app and I will update the local SQlite DB.
My questions:
Is this a good idea?
Are there any better ways to do this?
The data can be viewed (and edited) by a Zend Website, too.
Thanks in advance.
Best regards
Daniel
The way you put it seems optimal. Maybe you should set a flag or alert which is time or date related..in case the app starts too many times without internet.
>> For updates to your mobile app, you should consider the priority/urgency of having the same data on the server and your app.
> For the better ways to do it, you can opt the way which suits your requirement better.
To fetch the data in one thread and render it in another,
1. Write custom Asynctasks:
http://developer.android.com/reference/android/os/AsyncTask.html
AsyncTask Android example
OR
2. Use something like AsyncHttpClient: http://loopj.com/android-async-http/
where you get onSuccess and onFailure methods to work with the response.
The option 2. is better if you just want to fetch data without doing anything else, and work on it, or save it. For the same, you need to parse the response first.
To parse your data:
As your response is JSON format, you may be better off using Gson to map data and with custom model classes. Eg.:
Gson gson = new Gson();
ModelClass modelClass= new ModelClass();
modelClass= gson.fromJson(responseContent,ModelClass.class);
//where responseContent is your jsonString
Log.i("Web service response", ""+modelClass.toString());
More on: https://code.google.com/p/google-gson/
For Naming discrepancies(according to the variables in webservice), can use annotations like
#SerializedName.
Use a for each loop to verify/browse/access the data that would be populated in/as objects/fields of your model class:
Check these for doubts:
http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
How does the Java 'for each' loop work?
Now about saving your data:
>> It depends a lot on what the data from server is and how much data do you want to store.
In Android Storage Options:
http://developer.android.com/guide/topics/data/data-storage.html
a. There's Shared Preferences:
These are good for saving/storing data which would be relatively small in size and could be overwritten and fetched frequently. Eg. username, current user's details, password
http://developer.android.com/reference/android/content/SharedPreferences.html
How to use SharedPreferences in Android to store, fetch and edit values
b. Maintaining a database is good for the larger chunk needed in your app.
You can store, update or over-write the data according to your need. There can be multiple tables or more data could be stored in various fields.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.com/tutorials/AndroidSQLite/article.html
While you use Gson, you also have the option of populating the objects of model class and then storing that response in a String(maybe in SharedPreferences, depending on length/size) using gsonToJson method. I have used that, cheeky but effective.
You need to consider other stuff too, pertaining to UI and memory optimization, ListViews or layouts etc depending on your app and its control flow.
You could start a thread and get new data on loading the app. If you decide this path we had nice results with JSON using the Volley Project.
You can see a walk through here http://www.javacodegeeks.com/2013/06/android-volley-library-example.html
and get the code here
https://android.googlesource.com/platform/frameworks/volley/
Try using this library:-
https://github.com/Abhishekpalodath/FetchServerUtil-Android.git.
You can also load real-time data from the server by using this.
My app pulls data from the web server. The results are given to a ListView.
Question 1: In what format should I transmit data to minimize data usage ?
Question 2: Should I use a SqlLite database to store the results from the server and feed it to the ListView or can I load it a few (say 100) values to the an ArrayList and set it as the data source for the ListView for a better performance ?
Question 1: In what format should I transmit data to minimize data usage ?
Answer 1: Its depends on your server side data format. If it is in Xml format then you have to
use xml parsing. If its in Json format then you have to use JSON parsing. I suggest to you use JSOn parsing.
Question 2: Should I use a SqlLite database to store the results from the server and feed it to the ListView or can I load it a few (say 100) values to the an ArrayList and set it as the data source for the ListView for a better performance ?
Answer 2 : You can use HashMap arraylist to store your data and retrieve as easily from it. But if you have a bunch of data earlier said by you then you need to use SQLITE database. In which you have to store your all data in it. and after that retrieve from the Database.
Q1: you can use JSON or Xml for data transferring because these both are standards but JSON is widely used for data driven applications.
Q2: For Large data you can directly load data to list using lazy loading technique, or this is totally dependent on a person, if you store in database to use this data on multiple locations and for data storage, this depends on you
Data format
The JavaScript Object Notation (JSON) is widely used in transferring data over the net
Storage
That depends on what you're trying to do - if the data is only temporary in nature, what you're suggesting works. If it needs to be stored across application starts, you will need some kind of persistence. A database would be one way to do that.
Remember though, that if you place your data loading code in your activity's onCreate or onResume method (or anywhere in startup callbacks of the activity lifecycle), it will get loaded everytime your activity is created - even if the user just flipped the device. If the data takes long to load (which over the network can always be the case, even if it's just a few bytes), this can result in very bad user experience.
One way to deal with this would be using a custom Loader, which can exist separate from your activity and would be able to cache your previously loaded data.
Conceptually, doing this would require you to extend Loader, and override the onStartLoading method to begin loading your data from the network. You should probably also override onStopLoading and onCancelLoad to keep your app from needlessly shoveling data over the connection even if it's not wanted anymore.
Having done this, you would provide LoaderCallbacks (as shown in the link I gave you), and instead of creating a new CursorLoader in the onCreateLoader callback, create an instance of your own custom loader class.
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.
Its an application that is calling the ZXing barcode reader on button push, after scanning in shows you the code and you can put in an amount (how much you want to order from that product), then you go next scan, so on so on...I need to store this data. In my main window under the scan button, i will have a show scanned list, with all the scanned items plus the amount of each. And i will have a send button which sends a this list in a .txt or a .csv file via bluetooth to PC. And after closing the application with exit button, or with the back key, this list needs to be destroyed , so when i start the application again, there is a fresh, new list. First i thought, on using SQLite, but my costumer wants a simple program, without db. I could just need a few ideas on storing, data else how. Like an array list, but i dont know how hard is it to handle such a storing bethod, with editing or deleting and such. Or directly saving it into a .CSV file? Any idea is welcome.
Thanks you.
create a Bean class say Product which contains the attributes which you want to display on your list view.
Take a ArrayList
store the bean objects in arraylist and finally iterate it to do your calculations.
To get the CSV, you can call the to string on the instance of arraylist.
IMO using SQLite would be your best option, it does not add that much complexity to your app. Though if you really do not want to use it, Shared Preferences is an even simpler API for storing data.
I have an Android application which talks to a public Data-API by calling URLs. The API returns XML which describes search results or detailed Data of a particular dataset.
In my Application i retrieve this data and parse it to Java Objects to display them in a ListView for example.
edit: Just to make it clearer: In my Application you can search for music Artists and get their whole discographic information. I Retrieve the list of Releases and display them in a ListView. (No problem right here, because the results of the same search request can change any minute i have to retrieve this data everytime a search request is issued).
Now i have a list with all the LPs the Beatles produced (for example). I can then click one particular LP and view the Details such as the Release Year and the Tracklist.
What i want to cache is the Details data and i'm currently thinking of which is the best way to do this. I thought of:
Retrieving the XML data once and store the XML Data in the SQLite Database (that would imply, that i have to parse the data everytime i want to access it again).
Retrieving the XML data once, parsing it once and somehow store the serialized JavaObject into the SQLite Database as ByteStream. (so all the time consuming work would be done just once).
What do you think is the best version or is there maybe another better way to achieve caching the results?
serializing an object would be quick solution but that could not be effective solution. Every a time you need to load entire object, while in this case if you are storing your data set into database then, using cursor/queries data handling will be smoother.
CursorAdapter will allow you to plug database cursor directly to list in GUI. I would suggest you to use database approach.