Using AsyncTask to login user

5.2k Views Asked by At

I need to get user and save it to variable.

public class MainActivity extends ActionBarActivity 
{
    User user = new LoginTask2().execute("");
}

class LoginTask2 extends AsyncTask<String, Void, User> {
    private Exception exception;
    public String hash = "";

    protected String doInBackground(String... t) {
        RestClient restClient = new HttpRestClient();
        restClient.setUserAgent("bot/1.0 by name");

        // Connect the user
        User user = new User(restClient, "User", "somepass");
        try {
            user.connect();
            //hash = user.getModhash();
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(String string) {

    }
}

It looks like it work, but I don't know how to get user. With this code I get error:

Error:(49, 47) error: incompatible types
required: String
found:    AsyncTask<String,Void,String>

Could someone give me advice how to change code?

3

There are 3 best solutions below

0
On

Your async task is declared:

class LoginTask2 extends AsyncTask<String, Void, User> {
    protected String doInBackground(String... t) {

In order to avoid the incompatible types error, you need to make doInbackground return a User object.

    protected User doInBackground(String... t) {

See: http://developer.android.com/reference/android/os/AsyncTask.html

The code you have above:

User user = new LoginTask2().execute("");

Will also fail because you must execute the async task and then later use the return value. You could access the returned User object by setting it as a field in your MainActivity and then using that object later once AsyncTask completes.

1
On

Define your asyntask as inner class and onPostExecute assign the mUser class variable.

public class MainActivity extends ActionBarActivity 
    {
        User mUser;

        new LoginTask2().execute("");

    class LoginTask2 extends AsyncTask<String, Void, User> {
        private Exception exception;
        public String hash = "";

        protected User doInBackground(String... t) {
            RestClient restClient = new HttpRestClient();
            restClient.setUserAgent("bot/1.0 by name");

            // Connect the user
            User user = new User(restClient, "User", "somepass");
            try {
                user.connect();
                //hash = user.getModhash();
                return user;
            } catch (Exception e) {
                e.printStackTrace();
                this.exception = e;
                return null;
            }
        }

        protected void onPostExecute(User user) {
                mUser = user;
        }
    }
    }
0
On

The AsyncTask will not return the user variable when the execute method is called. So this following code will not work.

User user = new LoginTask2().execute("");

Let's make a few changes.

private class LoginTask2 extends AsyncTask<String, Void, User> {
    private Exception exception;
    public String hash = "";

    @Override
    protected String doInBackground(String... t) {
        RestClient restClient = new HttpRestClient();
        restClient.setUserAgent("bot/1.0 by name");

        // Connect the user
        User user = new User(restClient, "User", "somepass");
        try {
            user.connect();
            //hash = user.getModhash();
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(User user) {

    }
} 

As your AsyncTask belongs only for your this class, you should let it private. Also the return from the method doInBackground is the param from onPostExecute.

In order to save the user data you can take a few approaches:

One can use the onPostExecute method to save your data

protected void onPostExecute(User user) {
     //do save stuff
}

You could also call an method from your class for example:

public class MainActivity extends ActionBarActivity {
    User user = new LoginTask2().execute("");

    private void success(User user){
       //do save stuff
    }

    private void failure(){

    } 

    private class LoginTask2 extends AsyncTask<String, Void, User> {
        private Exception exception;
        public String hash = "";

        @Override
        protected String doInBackground(String... t) {
            RestClient restClient = new HttpRestClient();
            restClient.setUserAgent("bot/1.0 by name");

            // Connect the user
            User user = new User(restClient, "User", "somepass");
            try {
                user.connect();
               //hash = user.getModhash();
               return user;
            } catch (Exception e) {
               e.printStackTrace();
               this.exception = e;
               return null;
           }
       }

       protected void onPostExecute(User user) {
             if(user != null)
                success(user)
             else
                failure()
       }
    } 
}

You can also catch an failure :)