Saving my Fetched JSON data on the android phone - android

My android app is a news app that fetches JSON data as its content from a link.... I want the user to be able to store these contents on their android phone app directory for future viewing... Please how can I do this?

I'm not 100% sure if this is what you are asking, but it seems you want to store data in your app to the phone for quick access?
There are a few ways of doing this:
Shared Preferences
This is more for settings saved in the app in a key value pair style. You can save just about anything, though I wouldn't suggest it for long blocks of text as its main usage seems to be for small variable persistence.
Internal Storage
This is good for file storage. Probably a little closer to what you are looking for though it may be a little too intensive for something as storing blocks of text from news articles.
External Storage
Much the same as internal storage though through the SD card. This is also a file storage system.
SQLite DB
This would store all the data to a sqliteDB on the phone. This might be closer to what you are looking for if you have a bunch of articles to store and quickly access. This method might require some more work than the others but will probably be a better method in the long run.
Network
This is what you are doing to pull the data and probably not what you want to use to store it if you want to store it locally, but for others reading this. This would allow you to pull and push data from the net.
Here is a link to all of the Android storage options.

You can use SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.Editor.html
And make you json object implementing Parcelable
See How to use SharedPreferences in Android to store, fetch and edit values

Store data
SharedPreferences prefs = getSharedPreferences("DATA_FILE", 0);
SharedPreferences.Editor prefsE = prefs.edit();
String data = "some data I want to store on the phone for future use.";
prefsE.putString("NAMETOSAVEDATAUNDER", data);
prefsE.commit();
Get stored data
SharedPreferences prefs = getSharedPreferences("DATA_FILE", 0);
String data = prefs.getString("NAMETOSAVEDATAUNDER", null);

Related

Is Shared Preferences safe for private datas?

Im saving datas from my db/user into a gson formated ArrayList in SharedPreferences. Now my question :
Is it safe to save these datas (or data in general) into Sharedpreferences. Are users able to read these gson Arraylists out ? Maybe from SD card ,in a folder or somewhere else.
Thank you !
They are stored as xml files in your app directory, with permissions that allow only your app to access them. But on rooted device they are easily accessible. If you are concerned with security then you may use encryption, those projects might be usefull to you:
https://github.com/rtoshiro/SecureSharedPreferences
https://github.com/sveinungkb/encrypted-userprefs
still those projects does not give you 100% guarantee, hacker may decompile your apk and find keys used to encrypt shared preferences. So if your data is of use only for short time then remember to remove it from your device once user has finished using it. You may for example keep data on server and download it only when needed, caching locally only for short time - when its needed.
SharedPreferences is just a file located in phone private memory. So user can't access it but root can. Root can everything and many users have root's nowadays. You shouldn't store fragile data there
Android SharedPreference security
You can read all shared preferences Data
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types.
To see the information in the store you need to know the important thing from the data. This will make reading through the information super easy. But as simple as it's to keep a tiny bit of data as difficult it's to keep and browse large structured data since you need to define key for every data, in addition you can't really search inside the data except you've got a certain concept for naming the secrets.
Please read Android SharedPreference security

What is the best way to store string? SharedPreferences Set Collection or Files?

I have to store some data (string) in my Android app and I'm a dilemma. What is better solution ? Use Set Collection and keep it at SharedPreferences or I should save data to File and when I need it I have to read data from file and put it for example on ArrayList.
Depends on the quantity & complexity of the data. There is no straight answer to your question.
General approach: If the data are not too sensitive, small in quantity and more frequently used then you should go with SharedPreferences.
If your data is quite large lets say few hundred KBs of String then you should go with File.
SharedPreferences ultimately stores data into a file. The advantage of using SharedPreferences is that, the data is stored as a key value pair and can be retrieved easily using a key.
It depends on what you want to store.
Use SharedPreferences when you want store simple, prmitive data. Keep in mind that SharedPreferences will be available only for your app, so anothers apps cannot get data from it.
Use File when you have more complex data. You have to take care about how file is available to others. If you put it in sdcard root directory for example it will be available for everyone. If you put it in app package it will behave like SharedPreferences.

How to store data from my app

Actually i want to know how to store data from my app in the device so that i can review the store data when i run the application again..
means in simple terms i want to say that suppose i have text box where i write some information..now when i click the submit button, this information will be save,so that when i open the application the stored data should be appear in the text box..
In all terms i want to say that i just want to stored data in the way that we are using database for storing data..so please anyone suggest me how that can be done in android.
if possible show with an example
Regards
Anshuman
If you have to store small amount of data, you can use SharedPreferences in Android.
If the data that you have to store is big/complex enough, try using SQLite database.
Still need help?
UPDATE: There's a tutorial that I wrote to demonstrate how to use SQLite database. check it out here. Although it copies existing database into device's memory, but other versions of it, which create database through code can also be devised from it.
A better tutorial is here : http://www.vogella.com/tutorials/AndroidSQLite/article.html
1) If you want to store data in table format then you can use SQLite database in android
2) If you don't want to store data in table format then you can store in SharedPreference
more info about SharedPreference here and here
Android comes with a built in SQLite database that you can use. I advice you to go trough this notepad tutorial. It teaches the basics of using Android SDK including different states of the android application as well as how to use SQLite with Android.
For storing simple key = value pairs, you can use Properties.
For data storage as in a database, you can use sqlite on android.
Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.
Your data storage options are the following:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
Data Storage

store data without using database in android

i have to make an android application in which i need to download a lot of data from the server which is sent to me via XML. i then need to parse the XML and then display the extracted information.
To avoid making the application slow, i have decided to break my XML down into small parts.. so that i can only call the part that i want, this would limit the information that i am receiving.
My question is once that i have parsed the XML data where do i store it ( except for a db ) until my UI is rendered? On the iPhone there is something called user default where in we can store such information. What would be the equivalent in android?
thank you in advance.
Every thing you need should be right here: http://developer.android.com/guide/topics/data/data-storage.html. Internal storage is the closest to the iPhone equivalent.
You can use application preferences to store data as shown here (if the data is small enough).
Their code sample shows:
SharedPreferences gameSettings = getSharedPreferences("MyGamePreferences", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = gameSettings.edit();
prefEditor.putString("UserName", "Guest123");
prefEditor.commit();
I wouldn't do this for large datasets but it's a handy place to store data.
This gets removed when the application is removed too.
If you've got a lot of data, I'd suggest storing it on disk.
You could store it in a file.
You could store it in SharedPreferences
You could use a ContentProvider --> not recommended for temporary storage.
You could use a SQLiteDB --> I know you said you do not want to use this.

How to save data to internal storage in android app persistently between updates?

My android app saves user information as serialized objects in a ".ser" file that's saved to internal storage.
The data can be stored and retrieved and written just fine but whenever the user installs an update to the app, all the data is erased.
I assume the file is deleted upon installation of an update.
My question is: how do I save data to internal storage without it getting deleted when users install updates?
Do I have to use a different method, like SharedPreferences or SQLite?
or can my FileOutputStream save persistently through updates?
If you're worried about it getting deleted on internal memory, why not write to the ext?
Hello Boron I've been using SharedPreferences for the purpose you are explaining. I don't think SQLite would be preferable because you might want to save the images and large information like Bio of the user.
I store all my user data in shared preference and I am almost positive that the data persists even on updating the application. This should solve your problem.
When you have to store the images I would suggest you to convert the image to base 64 or some string and store in SharedPreferences this reduces the chances of deleting of the image from mobile by user accidentally.
Happy Coding

Categories

Resources