[英]Android: Best practice to use AsyncTask to load internet data periodly and retry it when fail to load
I think it is common to load data and refresh it after some time as the data may change. 我认为加载数据并在一段时间后刷新它很常见,因为数据可能会更改。 My question is about how to load it periodly and how to retry the load action when load failed . 我的问题是有关如何定期加载它,以及在加载失败时如何重试加载操作。
I tried to use AsyncTask to get this job done, but my code is ugly, I want to know if there are some elegant solution to deal with this. 我试图使用AsyncTask来完成这项工作,但是我的代码很丑陋,我想知道是否有一些优雅的解决方案来解决这个问题。
I have some concerns: 我有一些担忧:
Sounds complex, any good code example for this requirements? 听起来很复杂,对此要求有很好的代码示例吗?
Simple follow the following steps 简单按照以下步骤
Declare a timer task and schedule with Timer by method scheduleAtFixedRate(TimerTask task, long delay, long period)
通过scheduleAtFixedRate(TimerTask task, long delay, long period)
声明计时器任务并使用Timer进行scheduleAtFixedRate(TimerTask task, long delay, long period)
private class MyTask extends TimerTask { public void run(){ // Call to your async task by execute } } 私有类MyTask扩展了TimerTask {public void run(){//通过执行调用异步任务}}
declare a Handler 声明处理程序
When the Async Task done either in getting image or fail to do that.. send an empty message on the instance of the handler object 当异步任务完成获取图像或失败时..在处理程序对象的实例上发送空消息
in handler cancle the async task 在处理程序中取消异步任务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.