uma uma - 3 months ago 14
JSON Question

Retrive User Object form following String?

[
"User{id='0', username='f', password='f'}",
"User{id='0', username='t', password='t'}",
"User{id='0', username='uma', password='uma'}"
]


I create ShaerdPreferences database to store User object . following code do that stuff, and now I want to retrieve User object within
getUser method
. I used several method to do this but given error.I still couldn't take User object from above string . please wish some expert help to do this.

Full Code

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;



public class PreferenceUtils {


/**
* Save user credentials in shared preference
*
* @param context application context
* @param user logged-in user
*/
private static ArrayList<String> userDetail;


public static void saveUser(Context context, User user) {
SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();

if (null == userDetail) {
userDetail = new ArrayList<String>();
}

userDetail.add(user.toString());
JSONArray jsonAraay = new JSONArray(userDetail);
editor.putString("User", String.valueOf(jsonAraay));

.

editor.commit();
}


/**
* Get user details from shared preference
*
* @param context application context
* @return user object
*/
public static User getUser(Context context) throws NoUserException {
SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);

String userlist= preferences.getString("User","");
Log.e("object" , userlist);

//take user object and return


return new User("id ","UserName","Password");




}



}

Answer

You can use GSON library for this.

Add gson to your project by adding it as a jar or as a gradle dependency. This is the user guide for GSON. Add this to your build.gradle file.

compile 'com.google.code.gson:gson:2.7'

To Save,

public static void saveUser(Context context, User user) {
    SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    if (null == userDetail) {
        userDetail = new ArrayList<String>();
    }
    userDetail.add(user.toString());
    editor.putString("User", new Gson().toJson(userDetail));
    editor.commit();
}

Import for TypeToken is as follows.

import com.google.gson.reflect.TypeToken;

To retrieve,

public static List<User> getUser(Context context) throws NoUserException {
    SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);

    String userlist= preferences.getString("User","");
    Type collectionType = new TypeToken<List<User>>(){}.getType();
    userDetail = new Gson().fromJson(userlist, collectionType);
    return userDetail;
}

You can also modify the getUser method to get a particular User object from the list as follows.

public static User getUser(Context context, String id) throws NoUserException {
    SharedPreferences preferences = context.getSharedPreferences(context.getString(R.string.preference_file_key), Context.MODE_PRIVATE);

    String userlist= preferences.getString("User","");
    Type collectionType = new TypeToken<List<User>>(){}.getType();
    userDetail = new Gson().fromJson(userlist, collectionType);
    for (User user : userDetail) {
        if(user.getID().equals(id)){
            return user;
        }
    }
    return null;
}
Comments