farhad.kargaran
farhad.kargaran

Reputation: 2373

how to force android to read newst file from server?

In my app I read an xml online using following code and it works fine:

URL url = new URL("http://dl.1kolbe.ir/adv.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nl = doc.getElementsByTagName("item");

Node node = nl.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
final String adv_title = ((Node) nameList.item(0)).getNodeValue();

but when I change xml data on the server, my app display old xml data. I think I have to clear something like cache, but I don't know how!

Upvotes: 0

Views: 101

Answers (2)

nimbusgb
nimbusgb

Reputation: 403

If the app is not stopped completely ( which android does not readily do ) then it does not know that the content of your file has changed. You need to force a positive reload of the file either by monitoring the connection or the file timestamp or some other method.

Upvotes: 0

farhad.kargaran
farhad.kargaran

Reputation: 2373

I find a trick to solve it! I simply add some random and unused argument to url, so android think of it as a new url:

Time now = new Time();
now.setToNow();
String time = "" + now.hour + now.minute + now.second;
int time_int = Integer.parseInt(time);
URL url = new URL("http://dl.1kolbe.ir/adv.xml" + "?unused=" + time_int);

Upvotes: 1

Related Questions