0

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.

tinutomson
  • 329
  • 6
  • 12
  • 1
    What's the problem, exactly? Just call the appropriate method from `onPostExecute()` depending on the `Boolean` value received there. – Mike M. Apr 13 '16 at 06:00
  • @MikeM. : I have updated the question. Please go through it. – tinutomson Apr 13 '16 at 07:03
  • @cricket_007: updated. – tinutomson Apr 13 '16 at 07:05
  • What is your question? – barq Apr 13 '16 at 07:15
  • Question is more related to Java Generics. I am not able to create a class called ServerAPIAsyncTask with generic Datatype. If I declare something like Params it says Params is not recognized. I am a noob in generics and need some help in understanding how I can achieve this. – tinutomson Apr 13 '16 at 07:18
  • @user3400391 Instead of doing all this, i would recommend you to use AsyncHttpClient library http://loopj.com/android-async-http/ It has those success and failure methods built in, you need to over ride them, then you can create your own callback if you want. – rusted brain Apr 13 '16 at 07:47
  • @rustedbrain: A lot of my implementation is already based on java.net.URLConnection library. I don't think I can afford to change that. – tinutomson Apr 13 '16 at 07:52

3 Answers3

0

For Params, Progress and Result you need to pass in actual classes when you extend AsyncTask. Since you don't have classes in your class-path that match the names Params and Progress you get these errors.

public class ServerAPIAsyncTask extends AsyncTask<Void, Void, Boolean>{

  abstract public void onSuccessResult();
  public void onFailureResult() {
      int err = getErrorCode();
      showPopup(err);
  }

  protected void onPostExecute(final Boolean success) {
      if (success)
         onSuccessResult();
      else
         onFailureResult();
  }


}

For your second AsyncTask you should extend AsyncTask, not your own derived ServerAPIAsyncTask. Also, the first Parameter Garage needs to match the parameter you pass into doInBackground, see below:

public class GetCarDetailAPITask extends AsyncTask<Garage, Void, Boolean> {
  @Override
  protected Boolean doInBackground(Garage... params) {
     //call my api
  }

  protected void onPostExecute(final Boolean success) {
      super.onPostExecute(success);
  }

  @Override
  public void onFailureResult() {
      super.onFailureResult();
  }

  @Override
  public void onSuccessResult() {
      //Do app logic
  }
}
barq
  • 3,681
  • 4
  • 26
  • 39
  • If I inherit from AsyncTask, I will have not have onFailureResult to override or OnSuccessResult() to be implemented. This means that I will have to repeat a lot of code I have inside onFailureResult every time I extend a new API. – tinutomson Apr 13 '16 at 07:42
  • If you inherit from your other AsyncTask you can't change the types of Params, Progress and Result. To do your callbacks you can use an interface such as in this example: http://stackoverflow.com/questions/9963691/android-asynctask-sending-callbacks-to-ui I don't understand why you need two methods in your asynctask, though. You can simply call what you need on the UiThread from onPostExecute – barq Apr 13 '16 at 07:53
0

According to your comment, there you have generic AsyncTask example:

public class MyAsyncTask<A,B> extends AsyncTask<A, Void, B> {

    @Override
    protected B doInBackground(A... params) {
        return null;
    }

    // Other methods
}
Artur Szymański
  • 1,639
  • 1
  • 19
  • 22
0

You said this

//I get error Params, Progress not recognized.
public class ServerAPIAsyncTask extends AsyncTask<Params, Progress, Boolean>

Params and Progress are not real classes. They need to be real classes present in your package.

Some of them have different signature( Some have progress reporting, some others have different datatype being sent as Params).

Different datatype being sent as param? So set params to Object type. It is the superclass of all classes.

See this example, taken from AsyncTask documentation itself:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
  protected Long doInBackground(URL... urls) {
      int count = urls.length;
      long totalSize = 0;
      for (int i = 0; i < count; i++) {
          totalSize += Downloader.downloadFile(urls[i]);
          publishProgress((int) ((i / (float) count) * 100));
          // Escape early if cancel() is called
          if (isCancelled()) break;
      }
      return totalSize;
  }

  protected void onProgressUpdate(Integer... progress) {
      setProgressPercent(progress[0]);
  }

  protected void onPostExecute(Long result) {
     showDialog("Downloaded " + result + " bytes");
  }
}
rusted brain
  • 1,052
  • 10
  • 23