Android - result of ION caching

I am currently writing a small application that shows the current song being played in my local pub, loading the XML file generated by last.fm.

The problem is this: when synchronizing with xml, it does not get the new version, but instead uses the first loaded xml again and again. In the meantime, opening this link in a random browser gives the correct results. Maybe caching or lazy downloading, I don't know. I also don't know if this is related to ION or not.

I have currently fixed this with some code that clears the entire cache from this application before downloading, and it works very well, but since I probably want to expand the application, I will have to find another way to solve this problem.

My code is:

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();
}
}
+4
1

, http. , .noCache() .

. , , ..

.setLogging( "MyTag", Log.VERBOSE)

+10

Source: https://habr.com/ru/post/1536926/


All Articles