代码之家  ›  专栏  ›  技术社区  ›  AngelJanniee

我如何从SharedReference中将其作为响应对象?

  •  1
  • AngelJanniee  · 技术社区  · 7 年前

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

    然后我将对象另存为SharedReferences中的字符串,如下代码所示

    @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)在我的特定类中访问?因为,它正在返回字符串。但我的回答是字符串和数组。

    提前谢谢。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Rasoul Miri    7 年前

    可以将类中的值转换为字符串。

    拯救

     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);
            }
    
        2
  •  1
  •   Athar Iqbal    7 年前
    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);
    
        3
  •  0
  •   Neha    7 年前