为什么安卓的HttpURLConnection缓存InputStream的结果? [英] Why android HttpURLConnection cache the inputstream results?

查看:146
本文介绍了为什么安卓的HttpURLConnection缓存InputStream的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要得到一个XML文件,但它似乎被缓存。 还有我的code:

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();

谢谢!

推荐答案

您意识到你没有使用你的的HttpURLConnection ,对不对?如果你想获得的InputStream 使用的HttpURLConnection ,您需要调用

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());

另外,我认为,它的标准使用Apache的HttpClient对于这样的事情与Android,因为它是内置的,比标准的Java的东西更好的API。

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.

这篇关于为什么安卓的HttpURLConnection缓存InputStream的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆