2017-07-18 92 views
-1

我已經看到了大部分離線應用程序的例子,但是如果我的回答來自php api,我該怎麼辦?基本上我的應用程序登錄並提出了一個API的請求,我得到了迴應。假設我得到了作爲電子郵件和令牌的響應。那麼我應該如何在會話管理中使用它?以便用戶在他退出應用程序時不必一直登錄。在android中,如何保存來自php登錄腳本的響應作爲共享首選項?

+0

您可以使用sharedprefences保存電子郵件和令牌。如果您想知道該怎麼做,請在Google中搜索,找到您需要的信息 –

+0

您是否已將響應正文作爲String對象? – Roran

+0

@Roran是的,我正在接受響應並將其存儲在字符串變量中,事實上我在歡迎屏幕中使用它,但我想要的是在維護會話中使用這些響應。 – Robin10

回答

0

你可以存儲電子郵件和共享prefrence令牌這樣

當sharedpreferences用戶succefully登錄存儲數據這樣

public static final String MyPREFERENCES = "MyPrefs" ; 
public static final String email = "email"; 
public static final String token = "token"; 
public static final boolean isLogin = "islogin"; 

SharedPreferences sharedpreferences; 
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 

// store login data in sharedpreferences 
SharedPreferences.Editor editor = sharedpreferences.edit(); 
editor.putString(email, "[email protected]"); 
editor.putString(token, "123456789"); 
editor.putBoolean(isLogin, true); 
editor.commit(); //save data in sharedpreferences 

現在時檢查您的應用程序啓動用戶是登錄還是不喜歡這個

public static final String MyPREFERENCES = "MyPrefs" ; 
SharedPreferences sharedpreferences; 
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 


// get boolean from sharedprefrence 
public static final boolean isLogin = "islogin"; 
boolean login = prefs.getBoolean(isLogin, false); 

// check login status 
if(login){ 
    // user session available move to home screen 
}else{ 
// user does not login move to login screen 
} 
+0

謝謝,我會試試這個。 :) – Robin10

+0

和註銷按鈕我必須清除並提交編輯器並再次跳轉到登錄活動? – Robin10

+0

和順便說一句,我可以把字符串變量的值的一部分,像editor.putString(電子郵件,電子郵件); ?而不是像你給的那樣直接給[email protected]提供價值?因爲我的價值會根據用戶而改變(這很明顯)。 – Robin10