6

So I'm working on a project like this http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ which let's me to display my database's values to my android application GUI.

I need to save the database values I need to the android internal storage so I can access it even if my application is not connected to the server. Any help?

Thank you

6 Answers 6

12

You can write whole json as string in shared prefernces, and then get it and parse it to display in GUI even when device is offline:

String str = json.toString();
SharedPreferences sharedPref = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE );
SharedPreferences.Editor prefEditor = getSharedPreferences( "appData", Context.MODE_WORLD_WRITEABLE ).edit();
prefEditor.putString( "json", str );
prefEditor.commit();
Sign up to request clarification or add additional context in comments.

2 Comments

Note that this is only a good practice if you are dealing with relatively small JSON files.
6

If you chose local sqlite database as your solution, I wrote a lightweight utility for saving your json into a sqlite.

checkout git repo here. https://github.com/wenchaojiang/JSQL

You only need one line of code.

new JSQLite(yourJsonString, yourDB).persist();

Hope it helps

Comments

4

Consider using a local database to cache the server data locally, that is how most apps does it, here is a good tutorial for sqlite on android Android SQLite

If you use only one or few JSONObjects from the server you can use SharedPreferences, it is much easier and faster to edit/update. example

For more about android storage: Android Storage

Comments

2

I'm working on an application that connects to a php-mysql database in our server, instead of saving the result of the query in a file, you should save it (at least that is what I think so) in an internal Android Database (sqlite). There is a lot of information about databases in Android.

With this example you can see how to easily use sqlite and ContentProviders (a cleaner way of accessing data saved in your database.

In order to save correcty an JSONArray in your database i recommend you to use Jackson libraries in order to create objects from JSON making them easier to be saved.

Finally if the amount of information is relatively small you can use SharedPreferences aswell, this way the data can be accessed faster because it's saved in the mobile memory.

Hope it helps :)

Comments

2
    //if you have already the json string
    String str = json.toString();

    //if you want to convert list to json (with Gson):
    //foo - will be your list 
    String str = new Gson().toJson(foo );

   SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
   SharedPreferences.Editor prefEditor = sharedPref.edit();
   prefEditor.putString("pref_json_key", str);
   prefEditor.apply();

Comments

2

You can try to use with Realm.io A Simple Example like this:

public class City extends RealmObject {
  private String city;
  private int id;
  // getters and setters left out ...
 }

 // Insert from a string
 realm.beginTransaction();
 realm.createObjectFromJson(City.class, "{ city: \"Copenhagen\", id: 1 }");
 realm.commitTransaction();

Regards!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.