0

I am using AsyncTask to make an API call. My AsyncTask is supposed to send two images to a facial recognition service and return a Double contained in the JSON response from the service.

However, I keep getting this error regarding onPostExecute():

Error:(44, 5) error: method does not override or implement a method from >a supertype

I don't understand why I get this error.

Here is my AsyncTask subclass:

public class KairosManager extends AsyncTask<Bitmap, Void, JSONObject> {

    // MainActivity Context
    private Context mContext;

    public KairosManager(Context context){
        mContext = context;
    }


    // Call Kairos API and return JSONObject response to doInBackground()

    public JSONObject compareImages(Bitmap bm1, Bitmap bm2){
        // Kairos Call here!
        return jsonResponse;
    }


    // Take both images in the array and call compareImages which calls the service.

    @Override
    protected JSONObject doInBackground(Bitmap... params){
        Bitmap bm1 = params[0];
        Bitmap bm2 = params[1];
        return compareImages(bm1, bm2);
    }


    // Parse JSONObject response and return 'confidence' value to MainActivity

    @Override
    protected Double onPostExecute(JSONObject result){
        try{
            JSONArray TwoImages = result.getJSONArray(IMAGES);
            JSONObject image = TwoImages.getJSONObject(0);
            JSONObject subimage = image.getJSONObject(TRANSACTION);
            Double confidence = subimage.getDouble(CONFIDENCE);
            Log.d("Percent: ", confidence.toString());
            return confidence;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

Here is my MainActivity:

    Bitmap bm1 = ((BitmapDrawable) image1.getDrawable()).getBitmap();
    Bitmap bm2 = ((BitmapDrawable) image2.getDrawable()).getBitmap();

    KairosManager myKairosManager = new KairosManager(this);
    myKairosManager.execute(bm1, bm2);
Jacobo
  • 19
  • 4

1 Answers1

0

Your doInBackground method should return an object of type Double.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Right now doInBackground returns the JSON response and onPostExecute parses it and returns the value of interest. Are you suggesting that doInBackground should parse the JSONObject and return the value of interest too? Then my onPostExecute would be redundant. – Jacobo Dec 30 '15 at 19:13
  • @Jacobo Replace `, Void, Double>` with `, Void, JSONObject>`, the error will go away. – frogatto Dec 30 '15 at 19:15
  • I have made the changes you mentioned. Now I just have to figure out how to send the Double back to the MainActivity, but that is a different matter. Thank you for your help! – Jacobo Dec 30 '15 at 19:29
  • Use Java interfaces & callbacks. – frogatto Dec 30 '15 at 19:35
  • All I need is to TextView.setText(confidence.toString()); Can I do that directly from the AsyncTask class, or do I need to use Interfaces and Callbacks to send the confidence back to MainActivity and setText there? – Jacobo Dec 30 '15 at 19:43
  • @Jacobo If your `AsyncTask` class is an _inner class_ of your `Activity`, directly is possible. Otherwise, Java interface is required. – frogatto Dec 30 '15 at 20:21