重新执行异步任务

ReExecute AsyncTask

我的项目中有几个 AsyncTask 从一些给定的 api 中获取数据。 我遵循了以下步骤。

1) execute an Async Task and try to grab datas from there.
2) check conditions for internet and server down
3) if any issue in api or internet or server then show dialog [custom from self made class]
4) dismiss button for canceling the dialog and go back to the working stage of `app may be even by closing the activity`

我的问题是我想保留 Button "Retry" 以便重新执行 AsyncTask。 我搜索了通过 AsyncTask 但它似乎毫无价值,因为我得出结论 AsyncTask 无法通过。所以我不得不多次重复相同的代码cancel(true)

如果有人能用这个代码重用概念给我解决方案,那将非常有用。

您不能 "retry" 同一个 AsyncTask 对象 - 您只能调用 AsyncTask#execute() 一次。但是,您可以在用户决定重试下载任务时创建一个新的 AsyncTask 实例。

通过使用创建新对象 class 实现异步任务并执行该新对象的重试按钮解决了问题。