4

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.

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.

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.

My 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<String>() {
                    @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 && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {}
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}
}
Dennie
  • 743
  • 2
  • 13
  • 30

1 Answers1

9

Ion does in fact cache, according to the http spec. If you want to ignore caches, use the .noCache() method when building your request.

Tip: You can also turn on verbose logging in an ion request to see what is happening under the hood with regards to caching, etc.

.setLogging("MyTag", Log.VERBOSE)

koush
  • 2,972
  • 28
  • 31
  • How long does Ion cache requests for? The life of the app instance? And is there a limit on the size of the cache? How can the cache be cleared? – William Jul 27 '14 at 04:36
  • How do you define a cache? I was expecting to get some result from cache if I try to access the same service than a few minutes before... – Waza_Be Dec 04 '14 at 11:24
  • 2
    Ion caches according to the cache headers on the http response. Can clear the cache by using: Ion.getDefault(getContext()) .configure().getResponseCache() .clear(); – koush Dec 26 '14 at 08:44
  • Hi, first of all great lib Koush! Can I set config for how long I need to maintain cache? – Suyash Dixit Jun 19 '16 at 07:09