[英]How to handle a user session for android app
我正在創建一個應用程序,用戶可以在其中使用其登錄詳細信息進行登錄。 驗證用戶之后,用戶數據將從Web服務器下載到名為User的類中。 User類保存用戶信息,例如他們的姓名,年齡,生日,用戶名,電子郵件等。User類在每個應用程序生命周期中只會實例化一次。 以前,我曾將User類實例化為一個公共靜態變量,以便可以輕松地從其他活動和類中訪問它,但是由於缺乏封裝,這是較差的做法,因此我需要幫助弄清楚如何處理User類。
謝謝
編輯:這是我的登錄代碼:
private class LoginTask extends AsyncTask<String, JSONObject, Boolean> {
private String email, password;
private ProgressBar progressBar;
public LoginTask(ProgressBar progressBar, String email, String password) {
this.progressBar = progressBar;
this.email = email;
this.password = password;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
private HttpURLConnection loginConnection(String email, String encPass)
{
return Connection.getConnection(Links.LOGIN_SCRIPT, "email=" + email + "&password=" + encPass);
}
private HttpURLConnection saltConnection(String email) {
return Connection.getConnection(Links.GET_SALT, "request_salt=set&request_email=" + email);
}
private HttpURLConnection existsConnection(String email) {
return Connection.getConnection(Links.USER_EXISTS + "?email=" + email);
}
@Override
protected Boolean doInBackground(String... info) {
try {
String existString = Connection.getStringFromConnection(existsConnection(email));
int existInt = Integer.parseInt(existString);
if (existInt == Connection.SUCCESS) {
String saltString = Connection.getStringFromConnection(saltConnection(email));
JSONObject saltJson = new JSONObject(saltString);
String salt = saltJson.getString("salt"); //Perfect
if (salt.length() > 0) {
String encPass = Utils.SHA256(password + salt);
JSONObject encPassObj = new JSONObject();
encPassObj.put("email", email);
encPassObj.put("encPass", encPass);
publishProgress(encPassObj);
if (encPass.length() > 0) {
String loginString = Connection.getStringFromConnection(loginConnection(email, encPass));
int logInt = Integer.parseInt(loginString);
if (logInt == Connection.SUCCESS) {
//Globals.user = new User(email, encPass);
return true;
} else {
return false;
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onProgressUpdate(JSONObject... values) {
super.onProgressUpdate(values);
try {
JSONObject object = values[0];
setEmail(object.getString("email"));
setPassword(object.getString("encPass"));
preferences.saveLoginDetails(object.getString("email"), object.getString("encPass"));
} catch (JSONException e) {
e.printStackTrace();
}
}
//UI
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
progressBar.setVisibility(View.INVISIBLE);
}
}
LoginTask將基於成功登錄返回一個true或false值,並且電子郵件和加密的密碼將用於在Global類中創建一個新用戶。
例:
public class Global { public static User user = new User(email, password); }
我唯一的問題是用戶類的封裝。 我該如何改善?
由於您的應用程序只有一個用戶,因此您可以使用靜態字段創建一個非常簡單的類,該類只能初始化一次。
快速示例:
public class UserUtil {
private static String email;
private static int UID;
//etc
private static boolean initialized=false;
static public void initUserDetails(String email, int uid,//etc){
if (initialized==true)
throw new IllegalStateException("Error, User has already been initialized");
this.email=email;
this.UID=uid;
//etc
initialized=true;
}
//static public getters for private variables
}
試試這個...將數據存儲到本地數據庫。 同樣,通過本地數據庫,您不需要一次又一次地下載數據。
如果只有一個用戶,則需要將您的課程設置為單例課程。 這樣,您便知道該類只有一個實例,並且訪問該類的每個活動都將使用同一對象。
看一下這個問題: 創建一個可由Android中所有Activites訪問的對象
因此,可以說我們創建了一個Singleton類,如下所示:
public class SessionUser {
private static SessionUser instance = null;
protected SessionUser() {
// Exists only to defeat instantiation.
}
public static SessionUser getInstance() {
if(instance == null) {
instance = new SessionUser();
}
return instance;
}
private boolean isUserInSession()
{
//validation code
return isInSession;
}
}
您可以在任何這樣的類中使用它:
//Some code
if (!SessionUser.getInstance().isUserInSession())
{
//Go to login
}
//More code
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.