1

Iam storing my complex object by converting it to json using GSON in a shared preferences. But while retrieving it, not able to get the expected string.

CODE

Here holderListCustomizationMap is complex map of objects.

Setting shared preferences:

Gson gson = builder.create();
        SharedPreferences sh=getSharedPreferences("MYFILE",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sh.edit();
        String json = gson.toJson(holderListCustomizationMap);
        editor.putString("Holder",json);
        editor.apply();

While storing, json prints out to be the whole expected string.

Retrieving my map from shared preferences:

SharedPreferences sh = context.getSharedPreferences("MYFILE", Context.MODE_PRIVATE);
    String listCutomizatoinJson = sh.getString("Holder","");

when i print listCutomizatoinJson,getting this {} only not the whole json.

Yatin
  • 183
  • 1
  • 12
  • If the data is too big then you can try to save data to database. – Amsheer Apr 07 '15 at 07:25
  • 1
    Use `OrmLite` for easy and quick saving. `SharedPreferences` is not a place to store big objects but small and key values. – Shnkc Apr 07 '15 at 07:27

4 Answers4

1

Storing a large String to SharedPreferences is a long asynchronous operation. Check if your problem isn't that you are simply trying to read the value before it is saved. Also check whether you are using the same instance of SharedPreferences for both actions.

Lamorak
  • 10,957
  • 9
  • 43
  • 57
0

Create a sqLite database . shared preferences has a limitation of size . Generally it is used to store data when dealing with:

  1. small Data ( maybe score of a game, user name etc)

  2. In key-value pair

Pulkit Tyagi
  • 61
  • 11
0

Shared Preferences has no limitations.

The maximum size of a value in a SharedPreferences file is limited to the maximum size of the value you are attempting to store. (Meaning you can't put a String value that is longer than Strings can be in Java.)

Make sure that you tried to store correct value. May be you have problem with json parsing, but not with Shared Preferences.

Community
  • 1
  • 1
jimpanzer
  • 3,470
  • 4
  • 44
  • 84
  • json parsing has no issues because while printing json string after parsing, it is showing as expected. – Yatin Apr 07 '15 at 07:59
0

After a quick search I found that there doesn't seem to be limitations to shared preferences.

it looks like you are loading your loading your string incorrectly. You need to use the GSON method from this. It should look something like this.

GsonBuilder gsonb = new GsonBuilder();
Gson mGson = gsonb.create();
String listCutomizatoinJson = sh.getString("Holder", "");
String yourFile = mGson.fromJson(loadValue, String);

When you just obtain the string from your sharedpreferences you still need to convert it back to your original object

also when you store your value try using

editor.commit();

instead of .apply()

Community
  • 1
  • 1
NoSixties
  • 2,443
  • 2
  • 28
  • 65
  • yeah i know that, but listCutomizatoinJson string containing my json itself is coming blank. hence not able to recreate my object. – Yatin Apr 07 '15 at 08:03
  • @Yatin take a look at my edit. it might be the `.apply()`, I'm not sure tho since I always use `.commit()` the .commit() will give you a true when it was succesfull – NoSixties Apr 07 '15 at 08:08
  • There is not a big difference between `commit` and `apply`, only that `commit` returns whether the action was successful. – Lamorak Apr 07 '15 at 08:13
  • Yeah I just googled it myself. But it seems like his object isn't getting stored at all so with using `commit` we'll atleast know for sure nothing goes wrong when storing the value – NoSixties Apr 07 '15 at 08:17