Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ION caches result

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();
}
}
like image 808
Dennie Avatar asked Dec 25 '22 10:12

Dennie


1 Answers

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)

like image 160
koush Avatar answered Jan 12 '23 02:01

koush