3

i'm trying to get a xml file but it seems to be cached. there's my code:

URL url = new URL("http://delibere.asl3.liguria.it/SVILUPPO/elenco_xml.asp?rand=" + new Random().nextInt()+"&Oggetto=" + text +"&TipoDocumento="+tipoDocumento);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setDefaultUseCaches(false); 
urlConn.setAllowUserInteraction(true);
urlConn.setDoInput(true);
urlConn.setDoOutput(true);   
urlConn.setUseCaches(false);
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("Pragma", "no-cache");
urlConn.setRequestProperty("Cache-Control", "no-cache");
urlConn.setRequestProperty("Expires", "-1");
urlConn.setRequestProperty("Content-type", "text/xml");     
urlConn.setRequestProperty("Connection","Keep-Alive"); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new BufferedInputStream(url.openStream());
Document doc = db.parse(is);
doc.getDocumentElement().normalize();

thanks!

pino zulpo
  • 31
  • 1
  • 2
  • URL url="your url.com?x=xx"; InputStream is = null; is = url.openConnection().getInputStream(); try this dude.. then convert input stream to string – Ganapathy C Feb 22 '11 at 14:35

2 Answers2

1

You realize you aren't using your HttpURLConnection, right? If you want to get the InputStream using the HttpURLConnection, you need to call

InputStream is = new BufferedInputStream(urlConn.getInputStream());

Also, I believe that it's standard to use Apache HttpClient for this sort of thing with Android, since it's built in and a much better API than the standard Java stuff.

ColinD
  • 103,631
  • 27
  • 195
  • 199
  • 1
    you are right. i forgot the strange thing is it's working on emulator, but not on the phone (a htc desire)! – pino zulpo Feb 22 '11 at 14:47
  • and of course, i forgot to add the code you remember me up on the question. – pino zulpo Feb 22 '11 at 14:49
  • 7
    A more recent [android blog post](http://android-developers.blogspot.com/2011/09/androids-http-clients.html) says you should be using Apache HttpClient only for Froyo and below, but HttpURLConnection for everything after Froyo. – Gordon Glas Jul 07 '12 at 13:18
0

It looks like they have a solution with HttpClient that works. You might try it, and HttpClient gives a bit more flexibility with other issues

Community
  • 1
  • 1
mezmo
  • 2,351
  • 19
  • 22