MJDude
MJDude

Reputation: 73

Android doesn't grab new json file

I am developing an app that grabs a new json file every time is starts. But I've found that even though I delete the file and download the new one, the phone somehow still ends up with the old file.

Delete code:

if (test.exists()) {
            test.delete();
            Log.i(TAG, "Deleting File");
        } else {
            Log.i(TAG, "File does not exist");
}

Download Code:

DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));
    request.setTitle("stations.json");
    request.setDescription("File is being downloaded.....");
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
    request.setDestinationInExternalFilesDir(getApplicationContext(), null, "stations.json");
    request.setVisibleInDownloadsUi(false);


    manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    jsonRef = manager.enqueue(request);

I've deleted the file myself from the storage. It still grabs the old file. I've checked in a browser if the server version is updated. It was. I'm at a loss as to what may be happening. If the Stack community has any suggestions, it would be greatly appreciated. Thanks so much!

Upvotes: 1

Views: 51

Answers (1)

MJDude
MJDude

Reputation: 73

It appears that my phone was somehow caching the file. It would see that I was downloading the same file and would instead just give it to me. By giving it a new url, it tricked the app into thinking it was a new file.

In the end, what worked was to add:

?v=" + Math.random()

to the end of the file.

For Example:

String path = example.com/stations.json?v=" + Math.random();

Then download with:

    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(path));

Upvotes: 1

Related Questions