I have a set of APIs which are implemented using AsyncTask. Some of them have different signature( Some have progress reporting, some others have different datatype being sent as Params). But, all of these APIs return a boolean Result. On success, app Logic for successful calling of API is done. On failure, a generic error popup with error message is shown. Now I want to derive a class from AsyncTask in such a way that it implements a function onSuccessResult as well as overrides a function onFailureResult.
//I get error Params, Progress not recognized.
public class ServerAPIAsyncTask extends AsyncTask<Params, Progress, Boolean>{
abstract public void onSuccessResult();
public void onFailureResult() {
int err = getErrorCode();
showPopup(err);
}
@override
protected void onPostExecute(final Boolean success) {
if (success)
onSuccessResult();
else
onFailureResult();
}
}
Please note that I have to do all of this with two generic datatypes Params and Progress. How can I achieve this? I want to achieve this for two reasons. First I want to derive from this new class like this:
public class getCarDetailAPITask extends ServerAPIAsyncTask<Garage, void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
//call my api
}
@Override
protected void onPostExecute(final Boolean success) {
super.onPostExecute(success);
}
@Override
public void onFailureResult() {
super.onFailureResult();
}
@Override
public void onSuccessResult() {
//Do app logic
}
}
Secondly, it helps me to keep the onFailureResult logic at one place thus, not repeating it over and again.