我正在使用下面的代碼從服務器獲取數據。當數據在服務器上更新時,我從這個方法獲得舊數據。當我在網絡瀏覽器中使用相同的方法時,我得到更新的數據。獲取緩存的其餘數據android
即使當我停止應用程序並重新開始時,它反映了舊數據,但是當我使用任務管理器清理了所有任務後,我得到了新數據。
在設備上被緩存,因爲我每個
String response = null;
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
if (method == POST) {
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
OutputStream os = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(params);
writer.flush();
writer.close();
os.close();
} else {
urlConnection.setRequestMethod("GET");
}
int statusCode = urlConnection.getResponseCode();
/* 200 represents HTTP OK */
if (statusCode == HttpURLConnection.HTTP_OK) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
response = convertInputStreamToString(inputStream);
return response;
}
我在網上搜索後發現,使用高速緩存默認是開啓的,所以這兩個行會幫助時間作出新的請求數據
urlConnection.setUseCaches(false);
urlConnection.addRequestProperty("Cache-Control", "no-cache");
appende一些隨機獲取參數您的網址 – Amy