开发者

Why android HttpURLConnection cache the inputstream results?

开发者 https://www.devze.com 2023-02-13 04:37 出处:网络
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=

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.开发者_C百科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? 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消