The idea of AsyncTask is not to "get" a result but make something happen in the background without blocking the UI. Your approach will block the UI thread and until the server returns a response the app will be blocked and if that state stays for more than 5 secs the user will see a ANR
But to answer your question:
In oder to get boolean result from the AsyncTask you need to extend the correct class, in your case it is: AsyncTask<String, Void, Boolean>
, because the AsyncTask types are the following:
The three types used by an asynchronous task are the following:
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation.
So to answer your question, your code will be:
//USAGE
Login l = new Login();
Boolean valid = l.execute("user", "pass").get();
/* but the UI thread will be
blocked, meaning the following code will not be executed until the variable
valid is populated */
//AsyncTask
public class Login extends AsyncTask<String, Void, Boolean> {
public Login(Application application){
repository = new Repository(application);
}
@Override
protected Boolean doInBackground(String... strings){
try {
user = repository.getUser(strings[0], strings[1]);
if (user != null)
return true; //wont work
else {
return false;
}
}
catch(Exception e){
return null;
}
}
protected void onPostExecute(Boolean result) {
// this method is no longer needed since you will get the result directly
// from the doInBackground method
}
Nor for a solution that will actually work:
I suggest that every-time a user wants to log in the app should present a progressDialog
window informing the user that it has to do a time consuming task, and after the process is completed inform the user of the status.
In order to do this use the following code:
//USAGE
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Login l = new Login();
l.execute("user", "pass");
/* but the UI thread will be
blocked, meaning the following code will not be executed until the
variable valid is populated */
}
public void userLoggedIn() {
// do something when a user loggs in sucessfully
}
public void wrongCredentials() {
// alert the user that he didn't put in the correct credentials
}
//AsyncTask
public class Login extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// show a dialog window here when the AsyncTask starts
}
@Override
protected Boolean doInBackground(String... strings){
try {
user = repository.getUser(strings[0], strings[1]);
if (user != null)
return true; //wont work
else {
return false;
}
}
catch(Exception e){
return null;
}
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
// dismiss the dialog window here after the AsyncTask finishes
if (aBoolean) {
userLoggedIn();
} else {
wrongCredentials();
}
}
}