HttpURLConnection保持缓存

时间:2015-08-05 22:35:36

标签: java android caching httpclient httpurlconnection

我目前正在使用此代码从服务器获取数据

updateUserOptions

但它看起来像是保留缓存,也许不是我不确定,但如果我更改服务器上的数据并重新打开活动,它仍然在应用程序上保持不变。我之前一直在使用public static String getResponse(String URL) throws IOException{ try{ String response_string; StringBuilder response = new StringBuilder(); URL url = new URL(URL); HttpURLConnection httpconn = (HttpURLConnection) url.openConnection(); if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK){ BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream())); String strLine = null; while ((strLine = input.readLine()) != null){ response.append(strLine); } input.close(); response_string = response.toString(); } httpconn.disconnect(); return response_string; } catch(Exception e){ throw new IOException(); } } ,但由于HttpClient已将其更改为API 22,因此已弃用。那么有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:7)

您可以使用以下命令查看是否默认激活了缓存选项:

getDefaultUseCaches(); //or
getUseCaches();

here in the documentation.

如果您发现问题,那么您只需使用

进行更改即可
setDefaultUseCaches(boolean newValue) //or
setUseCaches(boolean newValue) // Uses a flag (see documentation)

here.