nida
nida

Reputation: 656

how to save JSONResponse in Session (SharedPreferences) using Android Native?

i am newbie in android. This is the Json which i am getting from service can someone please point me how can i save this JSONResponse in Session (Shared Preferences ) i already have defined this function in my SessionManager Class but i am failed to save it. JSON:

{
    "message": "Successfully Logged In.",
    "auths": "KONG,KONG-Surveyor",
    "user": {
        "view_all_proposals": "0",
        "role_id": "1",
        "contact_no": "020 8547 4333 (x354)",
        "date_modified": "2013-10-10 10:58:56",
        "status": "ACTIVE",
        "profit_margin_limit": "100",
        "cash_code": "KHO",
        "date_created": "2013-02-03 06:41:41",
        "user_initials": "KHO",
        "password": "f10343d1dc8d44c8935b356aa3f8aae2",
        "id": "27",
        "first_name": "Kong",
        "username": "kong",
        "is_admin": "0",
        "address": "Technology Manager",
        "email": "[email protected]",
        "proposal_review": "0",
        "last_name": "Hoang",
        "ldap_authentication": "1"
    },
    "status": true,
    "apkStatus": true,
    "apkFileDate": "2013-12-05 04:47:52",
    "apkFile": "2_SurveyAppNew.apk"
}

this is the function which i already defined in SessionManger Class:

 public static void setUserObject(Context c, String userObject) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(c);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("userObject", userObject);
        editor.commit();
    }

    public static String getUserObject(Context ctx) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        String userObject = pref.getString("userObject", null);
        return userObject;
    }

Upvotes: 2

Views: 3137

Answers (2)

Bhoomika Brahmbhatt
Bhoomika Brahmbhatt

Reputation: 7415

You can simply put an entire JSONObject as a string. Something like this:

String response = httpClient.execute(postMethod, resonseHandler);// your json parsing method

JSONObject jsonObject = new JSONObject(response);

setUserObject(context, jsonObject.toString(),"json_response");

And then you can retrive it like:

JSONObject jsonObj = new JSONObject(getUserObject(context),"json_response");

Change your method to:

 public static void setUserObject(Context c, String userObject,String key) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(c);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(key, userObject);
        editor.commit();
    }

    public static String getUserObject(Context ctx,String key) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(ctx);
        String userObject = pref.getString(key, null);
        return userObject;
    }

Books:

please Check answer of Jaguar for this Question.

Good Tutorial:

One of the Best Tutorial Series you can find here.

Video Tutorial Series.

Upvotes: 5

dipali
dipali

Reputation: 11188

JSONObject jObject_Main = new JSONObject(jsonStrng);
String jsonStr = jObject_Main.getString(jsonStringName);
JSONObject jsonObject = new JSONObject(jsonStr);
String message=jsonObject.getString("message");

SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(c);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("message", message);
        editor.commit();

first you should convert json string to json object and base on jsonobject you can use json string.

Upvotes: 0

Related Questions