title: Android: Best practice to use AsyncTask to load internet data periodly and retry it when fail to load
body: <p>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 .</p>
  
  <p>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.</p>
  
  <p>I have some concerns:</p>
  
  <ol>
  <li>Reload it <em>rapidly</em> when the first time it loads failed. For the words <em>rapidly</em>, I mean I do not want to wait the next refresh time point to load it again, because the data is not loaded the first time, so I need to load it as soon as possible.</li>
  <li>When it successfully been loaded, start the periodic refresh action. If loaded failed in one action, do not need to retry it as the data have been loaded before, I could just wait for the next refresh.</li>
  <li>Stop retry action if it exist or the periodic refresh when the activity goes invisible</li>
  <li>Resume retry action or the periodic refresh when the activity goes visible, still, retry action should happen <em>rapidly</em>, but the periodic refresh, I may want to check the last refresh time and to compare it with current time to determine whether the periodic refresh should start right now or later.</li>
  </ol>
  
  <p>Sounds complex, any good code example for this requirements?</p>