user2377651
user2377651

Reputation: 73

how to save json array to android internal storage

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

Upvotes: 6

Views: 25029

Answers (6)

Moises Portillo
Moises Portillo

Reputation: 838

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!!!

Upvotes: 2

DoronK
DoronK

Reputation: 4837

    //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();

Upvotes: 2

GingerJim
GingerJim

Reputation: 3907

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

Upvotes: 6

zozelfelfo
zozelfelfo

Reputation: 3776

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 :)

Upvotes: 2

Aneeq Anwar
Aneeq Anwar

Reputation: 1332

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();

Upvotes: 12

Mr.Me
Mr.Me

Reputation: 9276

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

Upvotes: 4

Related Questions