My Android app is a client that fetchs datas from a Web Service and show them to the user. I would like to know how to improve performance and avoid continuos connections.
Everytime I click the botton "show" for example I make the connection and get the json string. I know that my string will not change during the day (hopefully).
It's better to get the json string and save it in a text file, save the string on a DB (with "date" and "string" columns for example) and then deserialize it, or directly deserialize the string and save as proper objects in the DB?
Thanks a lot!
If it's a little string, say under 1K, put it in Shared Preferences. Otherwise, if your data is going to remain relevant for a few days, use a database. If your data is going to be pretty much thrown out and refreshed completely each day, and is a few KB, write it in a cache file.
Related
I have an sqlite database. The column where I want to save my JsonString in, has the data type TEXT. Everytime I want to insert a dataset I get no error until I try to read it out again, then I get an error that my curser may be at the wrong place. But when instead of a json give a usual string everything works fine. Since I don't really now what symbols might be in my json, is there at all a way to put a json into an sqlite database? (It is a large json so I can't really insert every value of it seperatly).
How can I save a JsonString else so that I can get I even if my application is closed?
I know I could put it in al file but since a database has a better structure and basiccly is a file too I would prefer to find a way working with the database.
Save as a string:
json.toString();
Although I would parse it, it is faster this way.
In my android app I load some data (JSON) from wed server. And I need to store that data.
All data, which I want to save - it is fixed number (for ex. 5) of JSONObjects with 30-50 fields.
Is this good idea to save each JSONObject in Preference? Or will be better to use SQLite?
note: why I ask?
save in Preference - implementation take 10 minutes.
use SQLite - implementation take one or two days, and required to
edit database, when I will add new fields.
I'd prefer database, and if you need 2 whole days for a reasonably simple database setup, you should perhaps take any chance to practice with it.
However, if you're sure you're never going to have any advantage of (complexer) queries, then saving it into a database might not give you any real advantages.
I would prefer sharedpreference here , because size of data is not too big. If size of data was too big and had many fields then database would be first choice. You can save the whole json String into preference and use it after parsing at run time.
My implementation choice would be a database table that stores the object as something you can then transform into a native intermediate object. By keeping the local data stored as JSON you're potentially locking yourself into keeping a JSON parser within the app for the foreseeable future even if your web service changes its format.
For example, if your web service changes to XML you're still going to have to keep the JSON parser in the app to handle those users who upgrade the app and have to do a data migration.
You could save JSON directly in SQLite database. If you parse data and then save in database then it would take some time for you to make everything work. But think about saving JSON directly in database. It will require two fields only in database link and json response.
Advantages:
- Big time saving
- You could save the same solution in your future apps for saving JSON in database.
Disadvantages
- You will not be able to query data
- You will have to parse every-time
I am developing a places of interest app which will display the list of places of interest in a location.
When user chooses one, it will display more information and address etc.
How do I store all this data? Currently I am using a text file to store all the data and subsequently when user chooses a place, it will parse the text file and retrieve the necessary data for display.
Any advice on what is a better way to do this? I looked at SharedPrefs, but it is more like storing "key-value" pair and in this case I need to store a large amount of data.
I want the info to be available even when the device is offline, thus I can't download from an online server upon request.
Any other way to do this?
You may store it to XML file using XML serializer, here is very good tutorial for learning that,
http://www.ibm.com/developerworks/library/x-android/
and it can be easily parsed using Java XPath Api. Have a look at this at parsing XML files
http://www.ibm.com/developerworks/library/x-javaxpathapi/
Use SQLite
It can store large data.
It is available offline.
All your problems will be sorted out.
Hre we have a wonderful tutorial for sq-lite
http://www.vogella.com/articles/AndroidSQLite/article.html
How about a relational database?
http://developer.android.com/training/basics/data-storage/databases.html
Take a look at Serialization. If you do not need database access, you could define a class what holds every information you need. Then, you can do the following:
when you need to save the datas, you serialize your object, dumping its content to a file, for example on the SD card
when you want to load the datas, you just load the above mentioned file, and get back everything from the dumped file
I am using this method in my app to cache some datas that would need internet access, so the user can still view it, and with proper implementation, this can work very nicely.
Use database, create table and insert all the data in it. When you need the data just fire the query, and you are done.
SQLite is fine for Android.
Depending on the type of data you want to store, you could use a SQLite Database (provided with Android) if it has a normal database structure. You could Serialize your data and save it in a raw or encrypted file, making you data implement Serializable.
I am working on a e-shop type application with more than 500 cathegories. Cathegory is basically a name-value pair and its not going to change. How should I store this data? Currently I am using string arrays in strings.xml resources file (one for names, one for values). The application now starts like 5 seconds or more, which is very slow. Is this because of too much resources? Do they all get loaded at startup?
Should I use database for this? Basically I need this data only later in the app, when creating a "query" , you select from all the cathegories, certainly not at start up..
If so, where should I put all those strings to have it bulk inserted at first app run? A class? wouldnt this still be slowing down every startup, as the class has to get loaded? Is it possible to load resource string array only in the activity where I will construct the query? (given they all get somehow loaded at startup, which it seems)..
Thanks!
You should use SQLite, and query strings whenever you want, hence it doesn't affect to the performance.
read more at:
http://www.vogella.com/articles/AndroidSQLite/article.html
and
http://developer.android.com/training/basics/data-storage/databases.html
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.