如果onPostExecute无法从服务器加载数据,我想在Dialoag框上显示重新加载按钮。那么如何在AsyncTask中重新启动或执行类似操作呢?
再调用一次:
new mAsyncTask.execute("");
在某些 if 块中,当数据未成功加载时,onPostExecute。
把它放在你的按钮的onClickListener中:
if(task.cancel(true)) {
YourTask task = new YourTask(this);
task.execute();
}
因此,单击按钮后,您将尝试取消当前执行的任务并启动一个新任务。
要重新启动 AsyncTask,您需要创建 asynctask 的新实例并调用 execute();
@Override
protected void onPostExecute(Response result) {
super.onPostExecute(result);
new YourAsyncTask().execute();
}
}