2017-07-26 76 views
1

我已創建的本地數據,如下面的代碼我如何從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)訪問?因爲它返回字符串。但我的迴應是字符串和數組。

在此先感謝。

回答

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

您可以將其保存爲SharedPreferences中JSON字符串的形式。然後在提取時你將能夠在你的課堂上得到它。

+0

我沒有得到。請詳細說明example – AngelJanniee

+0

好的,您可以將您的JSON響應轉換爲GSON,並且您可以使用完整的類。你可以參考這個鏈接https://stackoverflow.com/questions/22753719/how-to-parse-json-parsing-using-gson-in-android – Neha

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