0

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!

braX
  • 11,506
  • 5
  • 20
  • 33
farhad.kargaran
  • 2,233
  • 1
  • 24
  • 30
  • Have you looked here: http://stackoverflow.com/questions/5079204/why-android-httpurlconnection-cache-the-inputstream-results and here: http://stackoverflow.com/questions/2692814/httprequest-without-caching ? – sec_aw Dec 16 '14 at 10:14

2 Answers2

1

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);
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
farhad.kargaran
  • 2,233
  • 1
  • 24
  • 30
0

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.

nimbusgb
  • 393
  • 1
  • 3
  • 12