1
votes

We trying to retrieve from the internal storage. We want after registering e.g as "Oscar" then after logging in then it should show that I am logged in as "Oscar" on the main form. On the main form it should state that am logged in as "Any Username that has logged in".

This is what I have done so far:

String username = txtUsername.getText(); String password = txtPassword.getText();

Storage.getInstance().writeObject("Username", username);
Storage.getInstance().writeObject("Password", password);

Dialog.show("", "You Have Successfully Registered", "Okay Cool", null);
return false;

}

protected boolean onLoginLogin() {

TextField txtUsername = findTxtUsername();
TextField txtPassword = findTxtPassword();

String username = txtUsername.getText();
String password = txtPassword.getText();

String StorageUsername = (String) Storage.getInstance().readObject("Username");
String StroragePassword = (String) Storage.getInstance().readObject("Password");

if (username != null && username.equals(StorageUsername) && password != null && password.equals(StroragePassword)) {
    Dialog.show("Message", username + "\n" + "Logged In", "Okay", null);
    return false;
} else {
    Dialog.show("Error", "Incorrect Details", "Okay", null);
}
return true;

} }

1
You didn't include the portion where you show the login result. I suggest using the debugger and placing breakpoints to see what happened. I would also suggest using the Preferences API for this as it's much better at storing simple values - Shai Almog

1 Answers

-1
votes

I'll give an example:

You have the basic write and read methods:

public boolean putData(String name, Object o) {
    return Storage.getInstance().writeObject(name, o);
}

public Object getData(String name) {
    return Storage.getInstance().readObject(name);
}

You would then save the information as normal:

Map<String, String> userDetails = new HashMap<String, String>();
userDetails.put(UserStatic.PASSWORD, password);
userDetails.put(UserStatic.USERNAME, username);
MyStorageEnum.INSTANCE.putData(MyStorageStatic.USER, userDetails);

I recommend encrypting the password in some way if you are going to store it.

When the user logs in, you then just compare the information retrieved from the textfields to that of the storage.

Getting the stored data:

Map<String, String> userStore = (Map<String, String>) MyStorageEnum.INSTANCE.getData(MyStorageStatic.USER);

I then mapped this to a basic bean object:

public class User {
private String username;
private String password;
...

If you want to do validation, you could always see if the stored values exist:

public boolean exists(String name) {
    return Storage.getInstance().exists(name);
}

Then do a basic if statement:

if (user.getPassword().equals(password)
            && user.getUsername().equals(username)) {
    //doLogin
}

Part of the login process would be to store the user's username into a session, such as:

public enum Session {
INSTANCE;
private String username;
...

You can then use this Session enum in order to get the username whenever you wish to use it, as an example:

Session.INSTANCE.getUsername();