Why android HttpURLConnection cache the inputstrea

2019-04-11 16:24发布

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!

2条回答
Fickle 薄情
2楼-- · 2019-04-11 17:12

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.

查看更多
SAY GOODBYE
3楼-- · 2019-04-11 17:18

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

查看更多
登录 后发表回答