Best way to save a little amount of data - java

I'm currently developing an Android application where OAuth will be in use. In order to use OAuth, I need to be able to save the consumer's private key to access the protected resources. My question is, where would I want to save this tiny piece of data? I know that for most Android applications, data would be stored in a SQLite database. But for only a little bit of information, a string containing random letters and numbers, would SQLite be the best way to go? Or would it just be better to write the data to a file and save it on the file system? I don't know what the best practice for this would be, so hopefully I'll be able to get some insight.
Thanks a bunch guys!

You have a lot of choices:
http://developer.android.com/guide/topics/data/data-storage.html
However, if you just want to save one String (the OAuth key), I would suggest shared preferences. Example:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("oauthKey", yourOAuthKey);
// Commit the edits!
editor.commit();

I would save it in a file on the internal storage or in shared preferences.
Never the less you should encrypt the key for security reasons! On a rooted oder developer device it is no problem to access any file on the system.

SharedPrefs is much easier to work with than an sqlite database - especially for something so simple as you describe.

I guess a SharedPreference would do just fine.

Related

Saving data to file or database

I'm starting to work on a new Java desktop app that should help me and my colleagues learn vocabulary. It will contain around 700 words, some texts (that point to the words contained in them) and maybe some images (not sure about that part yet). The data will never change and I want the program to be able to run offline.
The question is: Should I use database, text file or serialize the data into file? Or perhaps if there is any other option I don't know about? If you could explain your choice in detail I would be glad.
If the data never changes and is only 700 words it would probably be easiest to use a file.
If your data was a bit more complex and had many fields and was being constantly updated, a database would be more preferable but a csv file could still be used.
Since you want to access this data offline and data never changes, I think the best option would be to just use text file, which will be more efficient in terms of access and speed.
Keep all the data in memory as Serializable Java objects, and store them serialized when your application is not running. Evaluate airomem - really nice solution that would perfectly work for you.

Best place to store custom objects so all activities can see them

I am an iOS developer who is trying to learn Android and I would like to make sure that I am following best practices.
I have custom objects that need to be accessible by 1 -> m activities and they need to be saved when the application closes. Currently I am using SharedPreferences, code below, to save them but I am not sure if it is the best route. Should I be using a singleton? Is there a better way?
SharedPreferences mPrefs = getPreferences(MODE_PRIVATE);
Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(userProfile);
prefsEditor.putString("UserProfile", json);
prefsEditor.commit();
gson = new Gson();
json = mPrefs.getString("UserProfile", "");
UserProfileObject outObject = gson.fromJson(json, UserProfileObject.class);
A singleton won't be saved when the application exits. Really your options are:
*SharedPreferences. Good for a small number of key/value pairs
*Database. Good for relational data
*File on disk, in whatever format you prefer. Good for any amount of data, but you may need to write a custom parser.
Storing json in a shared preference is a bit weird. Its not horrible so long as you aren't storing a lot of keys in there, but it makes it seem like you didn't know how to write a file.
#Gabe has provided a good answer. I am adding my 2 cents to it
I personally do not like to save serialised data in SharedPreferences. Instead I would use local db storage such as SQLite or Realm to store it. The reason being serialisation/deserialisation involves marshalling/unmarshalling objects using reflection which could potentially hit performance in a negative way.
In short, Use local db for storing complex / relational data and SharedPreferences for storing simple data
I think if your UserProfileObject can be easily constructed from Json and it doesn't contain any sensitive data (i.e. password), might be fine just having it in SharedPreferences (just save the json string like what you are doing).
Using a singleton SessionManager / ProfileManager class with that should be sufficient enough. Even though it might be used by 1 -> m activities, it only hits the SharedPreferences once using the singleton. Just make sure you keep the copy of the data in the singleton and in SharedPreferences in sync when there are changes. Or just dump the singleton all together and hit the SharedPreferences every time you need it (less worries about keeping copies in sync), don't think your use case will hammer it that much.

Load JSON from assets and add it to JSONObject: Android

I need to store the user input and other string to a local storing method to use it later. Also I need to update it with data every time the user inputs other preferences and read it. I've chosen JSON for this task, hope it's the best option.
My problem is that every time I close the app (or update the app version) the file gets reset.
I would save the JSONObject to the internal storage and update, change, read and check for its existence every time the app starts.
This file is the core of my app as it stores the preferences.
UPDATE:
I've used SharedPreferences to do my task:
SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
Ok, I've switched to using SharedPreferences. Thanks for your recommendations.

Persisting XML parsed object

I am using an xml parser to parse the login response from a request to a server. The xml content returns me sensitive data that uniquely identifies a client so i can fetch more details about the client in other part of my app later on.
In iOS, i would just create an object from the parsed values to hold the clientAuthenticationDetails and access it from anywhere else in the app using the appDelegate instance.
Is there a way for me to do the same in Android? I have read up on SharedPreferences but i am not sure if that is how others implement this sort of functionality or how secure it is since i do have sensitive client data in the response.
The easy option is to use Shared Preferences and write up some sort of method that will encrypt/decrypt the values in the preferences. Usually something like a DES encoder/decoder. you can find samples of using cypher anywhere.
The Harder option which usually doesn't make sense to implement is to store the account and credentials in the android AccountManager. I suggest going with the Shared Preferences option. Its enough in my opinion.

Android: different ways of storing and reading variable values?

I have about 16 variables in which the values need to be saved for later recall / editing. I've tried SQLite Database, since I am a novice at Android, it is just too hard for me. I will have to come back to SQL later. Are there any other ways to store these values for later recall / editing? Thank you! Maybe link me to a tutorial if possible? :-)
You might try using SharedPreferences.
SharedPreferences is another choice. Here is a very simple example
how to use it.
Storing 16 variables using SharedPreferences will make your code looks long. I advise you to try harder with SQlite database.

Categories