简体   繁体   English

使用AsyncTask登录用户

[英]Using AsyncTask to login user

I need to get user and save it to variable. 我需要获取user并将其保存到变量。

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 . 看起来不错,但是我不知道该如何获取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? 有人可以给我建议如何更改代码吗?

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. 为了避免不兼容类型错误,您需要使doInbackground返回一个User对象。

    protected User doInBackground(String... t) {

See: http://developer.android.com/reference/android/os/AsyncTask.html 请参阅: 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. 您可以通过将返回的User对象设置为MainActivity一个字段来访问它,然后在AsyncTask完成后再使用该对象。

Define your asyntask as inner class and onPostExecute assign the mUser class variable. 将asyntask定义为内部类,onPostExecute分配mUser类变量。

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;
        }
    }
    }

The AsyncTask will not return the user variable when the execute method is called. 调用execute方法时,AsyncTask将不会返回用户变量。 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. 由于AsyncTask仅属于此类,因此应将其设为私有。 Also the return from the method doInBackground is the param from onPostExecute. 方法doInBackground的返回值也是onPostExecute的参数。

In order to save the user data you can take a few approaches: 为了保存用户数据,您可以采取以下几种方法:

One can use the onPostExecute method to save your data 可以使用onPostExecute方法保存您的数据

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 :) 您也可以遇到失败:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM