我如何从SharedPreference中获取此响应对象?
问题描述:
我已创建的本地数据,如下面的代码我如何从SharedPreference中获取此响应对象?
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, "");
}
}
,然后我保存在sharedpreferences对象的字符串如下面的代码
@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");
}
}
现在我能得到这个对象的字符串将返回当我把这种
LocalDataSource.getInstance().getResposne("data");
现在,如何我可以在特定的类得到这个作为我的Response对象(LoadLookUpResponse.Data)访问?因为它返回字符串。但我的回应是字符串和数组。
在此先感谢。
答
您可以将类中的值转换为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);
}
答
您可以将其保存为SharedPreferences中JSON字符串的形式。然后在提取时你将能够在你的课堂上得到它。
答
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);
我没有得到。请详细说明example – AngelJanniee
好的,您可以将您的JSON响应转换为GSON,并且您可以使用完整的类。你可以参考这个链接https://stackoverflow.com/questions/22753719/how-to-parse-json-parsing-using-gson-in-android – Neha