AsyncTask has bugs - newbie

Eventually I want this method to look at some values ​​in a text document and return true for username and password. However, I am having some problems with the AsyncTask implementation. I tried to follow the guide http://developer.android.com/reference/android/os/AsyncTask.html but had no success.

The error I get in the return type of the doInBackground method is "The return type is incompatible with AsyncTask.doInBackground (String [])"

    private class AuthenticateUser extends AsyncTask<String, Integer, Boolean>
    {
        String user;
        String pass;

        protected void onPreExecute(String uname, String passwd)
        {
            user = uname;
            pass = passwd;
        }

        protected boolean doInBackground(String... strings)
        {
            return true;
        }

        protected boolean onPostExecute(boolean v)
        {
            return v;
        }
    } 

      

I know this is not a very good way to authenticate a user. I'm just trying to figure it out. Thank.

+3


source to share


2 answers


the problem is that AsyncTask extensions are generic and need three types: AsyncTask<Params, Progress, Result>

which can be Void or class, but not primitive data types.

So what's going on, you told the compiler that doInBackground is returning a primitive boolean, but was expecting an instance of the Boolean class. and thus you will get the "Return type is incompatible" error.



just change protected boolean doInBackground(String... strings)

to protected boolean doInBackground(String... strings)

and you should be fine.

+3


source


 new AuthenticateUser().execute(username, password);

      

...



 private class AuthenticateUser extends AsyncTask<String, Void, Boolean>
    {
        String user;
        String pass;

        protected Boolean doInBackground(String... strings)
        {
            this.user = strings[0];
            this.pass = strings[1];

            //authen
            return true;
        }

        protected void onPostExecute(Boolean result)
        {
             //do stuff when successfully authenticated 
        }
    } 

      

0


source







All Articles