在String后面检索用户对象表单?

时间:2016-08-11 05:35:53

标签: java android json string sharedpreferences

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

我创建ShaerdPreferences数据库来存储User对象。以下代码执行该操作,现在我想在 getUser method 中检索User对象。我使用了几种方法来做到这一点,但是给出了错误。我仍然无法从上面的字符串中获取User对象。请一些专家帮助做到这一点。

完整代码

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




    }



}

1 个答案:

答案 0 :(得分:1)

您可以使用GSON库。

通过将gson添加为jar或作为gradle依赖项,将gson添加到项目中。 This是GSON的用户指南。 将其添加到您的build.gradle文件中。

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

保存,

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

TypeToken的导入如下。

import com.google.gson.reflect.TypeToken;

要检索,

public static List<User> getUser(Context context) {
    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;
}

您还可以修改getUser方法以从列表中获取特定的User对象,如下所示。

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;
}