Android 使用httpUrlConnection恢复下载时,下载apk失败
我在使用HttpUrlConnection下载apk时遇到问题 我开发了一个大约30Mb的应用程序,我创建了一个管理器来检查最新版本并下载它。 由于其大小,我检查了下载的文件大小,并在连接被切断或应用程序被系统关闭时恢复下载 问题是,如果整个下载过程中断一次,则在安装下载apk时会发生解析错误。 这是错误消息: 分析错误:分析包时出现问题 这是我的下载代码:Android 使用httpUrlConnection恢复下载时,下载apk失败,android,download,Android,Download,我在使用HttpUrlConnection下载apk时遇到问题 我开发了一个大约30Mb的应用程序,我创建了一个管理器来检查最新版本并下载它。 由于其大小,我检查了下载的文件大小,并在连接被切断或应用程序被系统关闭时恢复下载 问题是,如果整个下载过程中断一次,则在安装下载apk时会发生解析错误。 这是错误消息: 分析错误:分析包时出现问题 这是我的下载代码: private File downloadApk(File aApkFile, String aUrl, long aStartPosit
private File downloadApk(File aApkFile, String aUrl, long aStartPosition, long aEndPosition) {
try {
URL url = new URL(aUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5 * 1000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Range", StringFormatter.format("bytes=%s-", aStartPosition));
conn.connect();
sendUpdateNotification(0, 100); // update notifaction info
InputStream inputStream = conn.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(aApkFile);
int currentPercent = 0;
long currentDownloadSize = aStartPosition;
byte readBuffer[] = new byte[1024 * 10];
int byteReadSize = 0;
while (!((byteReadSize = inputStream.read(readBuffer)) <= 0)) {
fileOutputStream.write(readBuffer, 0, byteReadSize);
currentDownloadSize += byteReadSize;
int index = (int) (currentDownloadSize * 100 / aEndPosition);
if (index != currentPercent) {
currentPercent = index;
sendUpdateNotification(currentPercent, 100);
}
}
fileOutputStream.close();
inputStream.close();
conn.disconnect();
return aApkFile;
} catch (MalformedURLException aE) {
aE.printStackTrace();
Log.e("Version", aE.getMessage());
} catch (IOException aE) {
aE.printStackTrace();
Log.e("Version", aE.getMessage());
}
return null;
}
private File downloadApk(文件aApkFile、字符串aUrl、长astarposition、长aEndPosition){
试一试{
URL=新URL(aUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
连接设置连接超时(5*1000);
连接设置读取超时(5*1000);
conn.setRequestMethod(“GET”);
conn.setRequestProperty(“连接”、“保持活动”);
conn.setRequestProperty(“范围”,StringFormatter.format(“字节=%s-”,aStartPosition));
连接();
sendUpdateNotification(01100);//更新NotiAction信息
InputStream InputStream=conn.getInputStream();
FileOutputStream FileOutputStream=新的FileOutputStream(aApkFile);
int currentPercent=0;
长currentDownloadSize=aStartPosition;
字节读取缓冲区[]=新字节[1024*10];
int byteReadSize=0;
当(!((byteReadSize=inputStream.read(readBuffer))删除以前的部分文件时:
FileOutputStream fileOutputStream = new FileOutputStream(aApkFile);
更改为附加模式:
FileOutputStream fileOutputStream = new FileOutputStream(aApkFile, true);
检查原始文件和下载文件的文件大小。每个字节都计数!FileOutputStream FileOutputStream=newfileoutputstream(aApkFile)
您在那里创建了一个新文件。因此您会丢弃以前的部分下载。您没有提到不可打包的apk的字节数比原始的少。请通知我们。另外,我找到了连接。getResponseCode()应该检查。在我的测试中,当我关闭wifi并在一段时间后打开它时,响应代码大部分是206。如果继续下载该响应代码,则apk文件大部分已损坏。因此我添加了'if(mConnection.getResponseCode()!=200){continue;}`确保连接稳定。确实。基本的东西。