title: Android - ION caches result
body: <p>I'm currently writing a small app that shows the current song playing in my local pub by downloading the last.fm-generated XML-file.</p>
  
  <p>The problem I have is the following: when syncing with the xml online it doesn't get the new version, but instead uses the first downloaded xml over and over again. In the meantime opening this link in a random browser does give the correct results. Could be caching or lazy downloading, I don't know. I also don't know if this is ION-related or not.</p>
  
  <p>I've currently fixed this with some code that clears the entire cache from this app before downloading, and this works pretty well, but since I'll probably want to expand the app I'll have to find another way to solve this problem.</p>
  
  <p>My code:</p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  
  private final static String nonXML = {the url to my xml-file}
  
  private String resultXml;
  
  private TextView artistTextView, songTextView, albumTextView;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      artistTextView = (TextView) findViewById(R.id.artistTextView);
      songTextView = (TextView) findViewById(R.id.songTextView);
      albumTextView = (TextView) findViewById(R.id.albumTextView);
      Button mainButton = (Button) findViewById(R.id.mainButton);
  
      mainButton.setOnClickListener(this);
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      update();
  }
  
  @Override
  public void onClick(View v) {
      update();
  }
  
  private void update() {
      deleteCache(this);
      getXML();
  
      XMLToClass convertor = new XMLToClass();
      NonPlaylist non = convertor.convert(resultXml);
  
      artistTextView.setText(non.getArtist());
      songTextView.setText(non.getSong());
      albumTextView.setText(non.getAlbum());
  }
  
  private void getXML() {
      try {
          Ion.with(getBaseContext(), nonXML)
                  .asString()
                  .setCallback(new FutureCallback&lt;String&gt;() {
                      @Override
                      public void onCompleted(Exception e, String result) {
                          resultXml = result;
                      }
                  }).get();
      } catch (InterruptedException e) {
          e.printStackTrace();
      } catch (ExecutionException e) {
          e.printStackTrace();
      }
  }
  
  public static void deleteCache(Context context) {
      try {
          File dir = context.getCacheDir();
          if (dir != null &amp;&amp; dir.isDirectory()) {
              deleteDir(dir);
          }
      } catch (Exception e) {}
  }
  
  public static boolean deleteDir(File dir) {
      if (dir != null &amp;&amp; dir.isDirectory()) {
          String[] children = dir.list();
          for (int i = 0; i &lt; children.length; i++) {
              boolean success = deleteDir(new File(dir, children[i]));
              if (!success) {
                  return false;
              }
          }
      }
      return dir.delete();
  }
  }
  </code></pre>