如何从SharedPreference获取此作为我的响应对象?

时间:2017-07-26 12:28:20

标签: android sharedpreferences retrofit2

我创建了本地数据,如下面的代码

public class LocalDataSource {

private SharedPreferences mSharedPreferences;
private static LocalDataSource sLocalRepository;

private LocalDataSource() {
    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.getApp());
}

public static LocalDataSource getInstance() {
    if (sLocalRepository == null) {
        sLocalRepository = new LocalDataSource();
    }
    return sLocalRepository;
}

public void saveResponse(Context ctx, String object, String key) {
    mSharedPreferences.edit().putString(key, object).apply();
}

public String getResposne(String key) {
    return mSharedPreferences.getString(key, "");
}

}

然后我将对象保存为共享首选项中的String,如下面的代码

@Override
        public void onSuccess(LoadLookUpResponse body) {
            super.onSuccess(body);

            if (body.responseCode != CommonResponse.Code.SUCCESS) {

                PopupErrorDialog.newInstance(body.responseMessage.header, body.responseMessage.message, body.responseMessage.btnText1, null, null, null).show(getSupportFragmentManager(), "popup_error");

            } else {
                mData = body.data;

                LocalDataSource.getInstance().saveResponse(getApplicationContext(), mData.toString(), "data");


            }
        }

现在我可以在调用此

时返回此Object String
LocalDataSource.getInstance().getResposne("data");

现在,我怎样才能将此作为我的Response对象(LoadLookUpResponse.Data)来访问我的特定类?因为,它正在返回String。但我的回答是字符串和数组。

提前致谢。

3 个答案:

答案 0 :(得分:1)

您可以将类中的值转换为String。

保存

 sharedPreferences.putString("data", new GsonBuilder().create().toJson(Resposne));

负载

String resposneString = sharedPreferences.getString("data", "");
        if (!userString.isEmpty()) {
            Constants.tutorialConfig = new GsonBuilder()
                    .serializeNulls()
                    .create()
                    .fromJson(resposneString
                            , DataResponce.class);
        }

答案 1 :(得分:1)

public class PreferenceUtil {
    public static void saveToPreferences(Context context, String preferenceName, String preferenceValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(preferenceName, preferenceValue);
        editor.apply();
    }

    public static void saveToPreferences(Context context, String preferenceName, boolean preferenceValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(preferenceName, preferenceValue);
        editor.apply();
    }

    public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        return sharedPreferences.getString(preferenceName, defaultValue);
    }

    public static boolean readFromPreferences(Context context, String preferenceName, boolean defaultValue) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        return sharedPreferences.getBoolean(preferenceName, defaultValue);
    }
}

// Use this class to write preference and read preference
// Create instance of PreferenceUtil class in your Activity
private PreferenceUtil mPreferenceUtil;
mPreferenceUtil.saveToPreferences(contex, prefKey, prefValue);

答案 2 :(得分:0)

您可以在SharedPreferences中以JSON字符串的形式保存它。然后在获取时你将能够在课堂上获得它。