简体   繁体   English

为什么android HttpURLConnection缓存输入流结果?

[英]Why android HttpURLConnection cache the inputstream results?

i'm trying to get a xml file but it seems to be cached. 我正在尝试获取一个xml文件,但它似乎是缓存的。 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! 谢谢!

You realize you aren't using your HttpURLConnection , right? 你意识到你没有使用你的HttpURLConnection ,对吗? If you want to get the InputStream using the HttpURLConnection , you need to call 如果要使用HttpURLConnection获取InputStream ,则需要调用

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. 另外,我认为将Apache HttpClient用于Android的这类事情是标准的,因为它内置了比标准Java更好的API。

It looks like they have a solution with HttpClient that works. 看起来他们有一个HttpClient的解决方案。 You might try it, and HttpClient gives a bit more flexibility with other issues 您可以尝试一下,HttpClient可以更灵活地解决其他问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM