3

Eventually I am wanting this method to look up some values in a text document and return true of the username and password are present there. However I am having some problems with implementing the AsyncTask. I have tried to follow the guide at http://developer.android.com/reference/android/os/AsyncTask.html but have had no success.

The error I am getting at the return type on 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 that this is not a good way to authenticate a user at all. I am just trying to figure this out. Thanks.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
Michael Murphy
  • 1,921
  • 2
  • 18
  • 21
  • are you trying to override `doInBackground` method of `AsyncTask`? – Abubakkar Feb 17 '13 at 11:16
  • 3
    just put `Override` before `doInBackground` this will give u automatically default structure of `doInBackground` method according to params passed in `AsyncTask` – ρяσѕρєя K Feb 17 '13 at 11:19
  • 1
    try changing `protected boolean doInBackground(String... strings)` to `protected Boolean doInBackground(String... strings)` using the class Boolean instead of the primitive boolean. – Oren Feb 17 '13 at 11:22

2 Answers2

3

the problem here is that AsyncTask Extensions are generic and need three types: AsyncTask<Params, Progress, Result> which may be Void or a class, but not primitive data types.

so what happens is you told the compiler that doInBackground returns a primitive boolean but it was expecting an instance of the class Boolean. and thus you get the "The return type is incompatible" error.

just change protected boolean doInBackground(String... strings) to protected Boolean doInBackground(String... strings) and you should be fine.

Oren
  • 4,152
  • 3
  • 30
  • 37
  • Thanks Oren, that has sorted it. However, I have another error that i forgot to mention. I am getting AuthenticateUser cannot be resolved as a type when I do: new AuthenticateUser.execute("http://path/to/file.txt"); – Michael Murphy Feb 17 '13 at 11:39
  • Do i heave to do this even if AuthentcateUser is a subclass? – Michael Murphy Feb 17 '13 at 12:09
  • `private class AuthenticateUser` the `private` part may have something to do with that. [this usage example](http://stackoverflow.com/questions/9671546/asynctask-android-example) may help you. note the asynctask's class is private as it resides in the scope of another, public, class. you can follow that design or just make your `AuthenticateUser` class public (depending on your expected usage). side note: if an answer on Stack Overflow helped you, you should accept it so it can help others as well. – Oren Feb 17 '13 at 12:27
0
 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 
        }
    } 
Intathep
  • 3,378
  • 2
  • 21
  • 28