2013-02-19 59 views
2

我在我的應用程序中使用Dropbox核心Api。當用戶在啓動時授權應用程序時,會創建一個密鑰和一個密鑰,允許應用程序與其保管箱進行交互。我做這SharedPreferences保存了這些按鍵:Android Dropbox API - 如何從SharedPreferences檢索授權密鑰

private void storeKeys(String key, String secret) { 
    // Save the access key for later 
    SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0); 
    Editor edit = prefs.edit(); 
    edit.putString(ACCESS_KEY_NAME, key); 
    edit.putString(ACCESS_SECRET_NAME, secret); 
    edit.commit(); 
    Log.d("DbAuthLog", key); 
    Log.d("DbAuthLog", secret); 
} 

的關鍵和祕訣都顯示在日誌窗口jkhfdsfueyuefd和yde767eyshy(爲當然的安全假鍵)。

現在,當我去取回他們的鍵,我這樣做:

private AccessTokenPair getStoredKeys() { //Need to fix this and test 

    SharedPreferences accessKey = getSharedPreferences(ACCESS_KEY_NAME, 0); 
    SharedPreferences secretKey = getSharedPreferences(ACCESS_SECRET_NAME, 0); 
    System.out.println(accessKey + "--" + secretKey + "from storeKeys"); 
    return null; 

的問題是,當我輸出getStoredKeys的項目,它們是作爲--android.app.SharedPreferencesImpl @返回[email protected] - 它現在與我存儲的內容相匹配。我在這裏做錯了什麼?

回答

2

您在正確訪問您的偏好設置。

SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0); 
String accessKey = prefs.getString(ACCESS_KEY_NAME); 
String secretKey = prefs.getString(ACCESS_SECRET_NAME); 
1

您正在將這些標記存儲爲SharedPreference中的字符串,您需要將它們還原爲字符串。

private AccessTokenPair getStoredKeys() { //Need to fix this and test 
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0); 
String accessKey = prefs.getString(ACCESS_KEY_NAME, ""); 
String secretKey = prefs.getString(ACCESS_SECRET_NAME, ""); 
System.out.println(accessKey + "--" + secretKey + "from storeKeys"); 
return null; 
+0

Geeze ......非常感謝你們!當我正在學習某些東西時,我討厭做些愚蠢的事情。我應該知道這一點。 – 2013-02-19 17:44:42