[Java for Android App Development] MaartenXDA:
Okay, here we go, the next section :D

Section 2: Parse Users

So, the ParseUser class is a really useful thingy for people who want to have some more control over the people who use their apps.

You can (for example) set a username, and then when the user wants to post something in the app, instead of him having to type his name all over again, the app can just use the ParseUser.getCurrentUser().getUsername() method, and it will automatigally get the user's name for him!

Now I'll show you how to setup a ParseUser:

Step 1: Setup everything in the same way as you did in Section 1

Step 2: First we'll have to make a ParseUser, by signing up. Here's a quick signing up code snippet:

ParseUser signup = new ParseUser();
signup.setUsername("testuser");
signup.setPassword("testpassword");
signup.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
// The user also is logged in now :)

} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
// Mostly the problem is that the username is already taken

}

But, ofcourse the user wants to choose his own username and password. For that you can use an EditText:

EditText username = (EditText) findViewById(R.id.USERNAMEEDITTEXTiD);
EditText password = (EditText) findViewById(R.id.PASSWORDEDITTEXTiD);

ParseUser signup = new ParseUser();
signup.setUsername(username.getText().toString());
signup.setPassword(password.getText().toString());
signup.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
// The user also is logged in now :)

} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
// Mostly the problem is that the username is already taken

}

But, if the user didn't type in anything, signin up throws a IllegalArgumentException. You can avoid that by using try and catch claws, like in this example:

EditText username = (EditText) findViewById(R.id.USERNAMEEDITTEXTiD);
EditText password = (EditText) findViewById(R.id.PASSWORDEDITTEXTiD);

try {

ParseUser signup = new ParseUser();
signup.setUsername(username.getText().toString());
signup.setPassword(password.getText().toString());
signup.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// Hooray! Let them use the app now.
// The user also is logged in now :)

} else {
// Sign up didn't succeed. Look at the ParseException
// to figure out what went wrong
// Mostly the problem is that the username is already taken

}
} catch (IllegalArgumentException IO {
// The user didn't type in anything, I suggest using a Toast to notify him

}

Now, if a user wants to post something using his account name, you can get his username using this:

ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.getUsername();

If your user wants to login, you can use this:

EditText username = (EditText) findViewById(R.id.USERNAMEEDITTEXT);
EditText password = (EditText) findViewById(R.id.PASSWORDEDITTEXT);

ParseUser.logInInBackground(username.getText().toString(), password .getText().toString(), new LogInCallback() {
public void done(ParseUser user, ParseException e) {
if (user != null) {
// Hooray! The user is logged in.

} else {
// Signup failed. Look at the ParseException to see what happened.
// The main cause is that the user didn't type in a correct username or password

}
}
});

And if you want to log the user out, you can do it very simply by calling:

ParseUser.logOut();

And that's it! You just learned how to signup a user, log him in, get his username and log him out!

Now it's time for the next section!