我正在尝试创建需要对用户进行身份验证的 Android 应用程序(通过 REST Web 服务)。该应用程序有多个活动和屏幕,都需要用户登录,登录后用户可以在网站上添加和编辑帖子。
我已经读过在“ConnectionManager”单例中使用 AndroidHttp 客户端是最好的方法。但是,我将在哪里存储用户详细信息(用户名、密码),这会在单例中吗?每次用户尝试编辑/添加内容时我应该进行身份验证吗?
我应该有这样的课程:
public class ConnectionManager {
private static ConnectionManager instance = null;
private AndroidHttpClient client;
private ConnectionManager() {
client = AndroidHttpClient.newInstance("Android-Connection-Manager");
}
public static ConnectionManager getInstance() {
if( instance == null ) {
instance = new ConnectionManager();
}
return instance;
}
public void authenticate(String username, String password) {
//Check for authentication here
}
}
每次用户做某事时调用下面的代码:
private static ConnectionManager conn = ConnectionManager.getInstance();
conn.authenticate();
或者
我应该将用户详细信息存储在单例中吗
public class ConnectionManager {
private static ConnectionManager instance = null;
private AndroidHttpClient client;
private AppUser mLoggedInUser;
private boolean mAuthenticated;
private ConnectionManager() {
client = AndroidHttpClient.newInstance("Android-Connection-Manager");
}
public static ConnectionManager getInstance() {
if( instance == null ) {
instance = new ConnectionManager();
}
return instance;
}
public void InitialiseUser(String username, String password) {
//Do login checks here then return true if logged in
mAuthenticated = true;
}
public boolean isAuthenticated() {
return mAuthenticated;
}
}