如何正确覆盖AsyncTask onPostExecute()方法?

时间:2015-12-30 18:47:03

标签: java android android-asynctask

我正在使用AsyncTask进行API调用。我的AsyncTask应该向面部识别服务发送两个图像,并从服务中返回JSON响应中包含的Double

但是,我一直收到有关onPostExecute()的错误:

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

我不明白为什么会收到此错误。

这是我的AsyncTask子类:

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();
        }
    }

}

这是我的MainActivity:

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

    KairosManager myKairosManager = new KairosManager(this);
    myKairosManager.execute(bm1, bm2);

1 个答案:

答案 0 :(得分:0)

您的doInBackground方法应返回Double类型的对象。