Android 如何从SharedReference中获取此作为响应对象?
我已经创建了如下代码所示的本地数据Android 如何从SharedReference中获取此作为响应对象?,android,sharedpreferences,retrofit2,Android,Sharedpreferences,Retrofit2,我已经创建了如下代码所示的本地数据 public class LocalDataSource { private SharedPreferences mSharedPreferences; private static LocalDataSource sLocalRepository; private LocalDataSource() { mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.g
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, "");
}
}
@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");
}
}
然后我将对象另存为SharedReferences中的字符串,如下代码所示
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, "");
}
}
@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");
现在,如何将其作为响应对象(LoadLookUpResponse.Data)在特定类中访问?因为,它正在返回字符串。但我的回答是字符串和数组
提前谢谢 您可以将其以JSON字符串的形式保存在SharedReferences中。然后,在获取时,您可以在类中获取它。您可以将类中的值转换为字符串 拯救 装载
我没有得到。请用exampleOk详细说明,您可以将JSON响应转换为GSON,为此,您可以使用完整的类。你可以参考这个链接
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);