0

我在 Android 中有一个基于异步改造的 API 调用,需要等待数据库调用,直到 API 调用完成,这样我才能确定正确的数据被输入到数据库中。

我读到您可以使用 Futures 来完成此任务,但是在我当前的实现中,我得到了一个空指针异常。

下面是API方法:

public Future<Void> postPrintMode(String authorization, final int userid, String deviceuid, final Map payload){
    api.postPrintMode(authorization, userid, deviceuid, payload, new Callback<PrintMode>() {

        @Override
        public void success(PrintMode printMode, Response response) {

            if (printMode.get_id() != 0) {
                dbOps.writePrintMode(userid, printMode);
                bus.getBus().post(new EVTNewPrintMode(printMode));
            }
        }

        @Override
        public void failure(RetrofitError retrofitError) {
            retrofitError.printStackTrace();
            APIUtils.showAPIResponseBody(retrofitError);
        }
    });

    return null;
}

在我继续读取数据库结果之前,我要确保异步代码执行的块在这里。

Future<Void> f = APIExec.getInstance().postPrintMode(IConstants.authorization, IConstants.userId, IConstants.deviceUid, payload);
    // here I get the null pointer exception
    f.get();
    // the code below needs to be executed after the postPrintMode(...) async method;
    DBPrintMode printMode = APIDBOps.getInstance().readPrintModeByPrintModeID(6);
    assertNotNull("Print Mode does not exist", printMode);
4

2 回答 2

0

您可以使调用public Future<Void> postPrintMode方法的类实现new Callback<PrintMode>接口。之后,您可以postPrintMode从中获取并将对自身的引用传递给方法。

这是一个粗略的例子(代码未经测试)

class Foo implements Callback<PrintMode> {

        Future<Void>  f;

        public Foo(){
            f = APIExec.getInstance().postPrintMode(IConstants.authorization, IConstants.userId, IConstants.deviceUid, this);
        }

        @Override
        public void success(PrintMode printMode, Response response) {

            if (printMode.get_id() != 0) {
                dbOps.writePrintMode(userid, printMode);
                bus.getBus().post(new EVTNewPrintMode(printMode));
            }
            if (f != null){
                f.get();
                // the code below needs to be executed after the postPrintMode(...) async method;
                DBPrintMode printMode = APIDBOps.getInstance().readPrintModeByPrintModeID(6);
                assertNotNull("Print Mode does not exist", printMode);
            }
        }

        @Override
        public void failure(RetrofitError retrofitError) {
            retrofitError.printStackTrace();
            APIUtils.showAPIResponseBody(retrofitError);
        }

}
于 2015-09-01T14:49:16.577 回答
0

创建一个 AsyncTaskThread 类,如下所示,

public class AsyncTaskThread extends AsyncTask<Void, Void, Void> {

Context context;
Handler myHandler;

public AsyncTaskThread( Context activityContext, Handler handler ) {
    this.context = activityContext;
    this.myHandler = handler;
}

@Override
protected void onPreExecute() {
    // before starting thread you can pre process few things here if needed
}

@Override
protected Void doInBackground(Void... params) {

// 在这里做任何你想做的事,比如调用你的 API 并返回你的结果 return null; }

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    // after doIn Background this method is called which will set the meesage object and give it back to handler
    Message message = new Message();
    message.obj = result;
    myHandler.sendMessage(message);
}

}

将此异步类称为,

new AsyncTaskThread(this, new MyHandler()).execute();

并且您必须将此处理程序类放在您放在上面的类中,根据您在句柄中获得的结果,您可以执行进一步的操作,

    private class MyHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
    }
}
于 2015-09-01T15:10:03.363 回答