简体   繁体   English

如何在Android中清除HttpURLConnection的缓存?

[英]How to clear HttpURLConnection's cache in Android?

I have an application which sends a GET request to a link and get some JSON data back from the server, then saves them in a List of Posts (a custom Java object) and show the result in activity... 我有一个应用程序,它向链接发送一个GET请求并从服务器返回一些JSON数据,然后将它们保存在Posts列表(一个自定义Java对象)中,并在活动中显示结果...

The problem is, it shows me the cached result! 问题是,它显示了缓存的结果! So if I change something in that JSON file, it takes a while for my application to show those changes! 因此,如果我在该JSON文件中更改某些内容,我的应用程序需要一段时间才能显示这些更改!

I'm using HttpURLConnection for connecting to the server, and I also tried using .setUseCaches : 我正在使用HttpURLConnection连接到服务器,我也尝试使用.setUseCaches

URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setDefaultUseCaches(false);
connection.setUseCaches(false);
...
...

But it didn't work either... 但它也不起作用......

What should I do? 我该怎么办?

EDIT (I forgot to mention): 编辑(我忘了提):

After I change that value and open the JSON file in PC's browser (any browser), I see the new result, not the cached one... But Android uses the cached version! 在我更改了该值并在PC浏览器(任何浏览器)中打开JSON文件后,我看到了新结果,而不是缓存的结果......但Android使用缓存版本!

try this: 尝试这个:

    //create connect http
    URL oURL = new java.net.URL("http://some.site.url");
    HttpURLConnection con = (HttpURLConnection) oURL.openConnection();

    // set none cache
    con.setRequestProperty("Cache-Control", "no-cache");

    con.setDefaultUseCaches(false);
    con.setUseCaches(false);

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

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