我的代码中有第三方异步块。我怎么知道它何时结束?

时间:2017-03-08 01:17:37

标签: java android multithreading asynchronous retrofit

我正在使用Retrofit。并且call.enqueue以异步方式执行。我有一个代码块,只有在call.enqueue成功执行后才能执行。

call.enqueue(new Callback<Event>() {
        @Override
        public void onResponse(Call<Event> call, Response<Event> response) {
            returnedEvent =  response.body();

        }

        @Override
        public void onFailure(Call<Event> call, Throwable t) {
            Toast.makeText(ViewEventActivity.this, "There was some internal error. Please try again!", Toast.LENGTH_SHORT).show();
        }
    });
    //code block to be executed after the thread has been executed.
    mTitle = (TextView) findViewById(R.id.event_title);
    mStartTime = (TextView) findViewById(R.id.startsAt);
    mEndTime = (TextView) findViewById(R.id.endsAt);
    mEventTitle.setText(event.getTitle());

提前致谢!

1 个答案:

答案 0 :(得分:2)

onResponse or onFailure被执行时意味着call.enqueue已成功执行。您应该在此处添加您的区块。

call.enqueue(new Callback<Event>() {
    @Override
    public void onResponse(Call<Event> call, Response<Event> response) {
        returnedEvent =  response.body();
        //code block to be executed after the thread has been executed.
        mTitle = (TextView) findViewById(R.id.event_title);
        mStartTime = (TextView) findViewById(R.id.startsAt);
        mEndTime = (TextView) findViewById(R.id.endsAt);
        mEventTitle.setText(event.getTitle());
    }

    @Override
    public void onFailure(Call<Event> call, Throwable t) {
        Toast.makeText(ViewEventActivity.this, "There was some internal error. Please try again!", Toast.LENGTH_SHORT).show();
    }
});