应用程序似乎缓存http数据

时间:2012-10-30 11:09:14

标签: android

我正在为我公司开发供内部使用的应用程序编写自动更新程序。应用程序存储在外部Web服务器上。我遇到的问题是每次下载尝试都会检索不再在服务器上的旧数据。如果我使用手机的浏览器(或我的PC浏览器),新数据会正确下载。我正在下载APK和JSON文件。

我在AsyncTask doInBackground中尝试了两种不同的方法(在SO上都找到了):

HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
request.addHeader("deviceId", "xxxxx");
String result = "";
ResponseHandler<String> handler = new BasicResponseHandler();
result = httpclient.execute(request, handler);
httpclient.getConnectionManager().shutdown();
return result;

URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.setDefaultUseCaches(false);
connection.connect();

// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();

//download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(downloadDir + "/" + name);

byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
   total += count;
   // publishing the progress....
   publishProgress((int) (total * 100 / fileLength));
   output.write(data, 0, count);
}

output.flush();
output.close();
input.close();

我在两部手机上试过这个。第一次下载很好,然后手机重新下载第一个下载的版本。我怀疑缓存是在服务器上,因为它只发生在我的应用程序中。另外,我肯定不会读旧文件;我已经检查过并尝试下载到字符串中。令我困惑的是,我在不同的应用程序中使用了上面的第一个方法,它运行得很好。这可能是一个缓存问题吗?如果是这样,我如何强制它清除旧数据?重命名服务器上的文件可以解决这个问题,但是有些文件的名称不能更改,而且重命名文件也是一个相当难看的解决方案。

0 个答案:

没有答案