下載完安裝的時候出現(xiàn)“解析包時出現(xiàn)問題”
請問下,為什么我下載下來(apk文件)的文件大小都是對的,也關(guān)閉了。但是安裝的時候總是“解析包時出現(xiàn)問題”沒法安裝呢
@Override
public void run() {
HttpURLConnection connection = null;
InputStream inputStream = null;
RandomAccessFile file? = null;
try {
if(!threadDAO.exists(mThreadInfo.getUrl(), mThreadInfo.getId())) {
threadDAO.insert(mThreadInfo);
}
URL url = new URL(mThreadInfo.getUrl());
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
connection.setRequestMethod("GET");
int start = mThreadInfo.getStart() + mThreadInfo.getFinished();
int finished = mThreadInfo.getFinished();
connection.setRequestProperty("Range", "bytes="+start+"-"+mThreadInfo.getEnd());
connection.connect();
if (connection.getResponseCode() == HttpStatus.SC_OK || connection.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT) {
inputStream = connection.getInputStream();
file = FileUtils.getSDCardFile(mFileInfo.getFileName(), "rwd");
file.seek(start);
byte [] buffer = new byte[1024];
int len = 0;
long currentTime = System.currentTimeMillis();
while ((len = inputStream.read(buffer)) != -1) {
file.write(buffer, 0, len);
finished += len;
if (!DownloadService.paused) {
if (System.currentTimeMillis() - currentTime > 500 || finished == mFileInfo.getLength()){
currentTime = System.currentTimeMillis();
Intent intent = new Intent(DownloadService.ACTION_UPDATE_PROGRESS);
intent.putExtra("progress", (int)(finished * 100 /mFileInfo.getLength()));
mContext.sendBroadcast(intent);
}
}
if(DownloadService.paused){
threadDAO.update(mThreadInfo.getUrl(), mThreadInfo.getId(), finished);
}
}
threadDAO.delete(mThreadInfo.getUrl(), mThreadInfo.getId());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
if (file != null) {
try {
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2015-05-23
可以參考下:
package com.download.services;
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.apache.http.HttpStatus;
import android.content.Context;
import android.content.Intent;
import com.download.db.ThreadDAO;
import com.download.db.ThreadDAOImpl;
import com.download.entities.FileInfo;
import com.download.entities.ThreadInfo;
/**
?* 下載任務(wù)類
?*
?*/
public class DownloadTask {
private Context mContext = null; //程序上下文
private FileInfo mFileInfo = null; //下載文件的信息
private ThreadDAO mDao = null; //數(shù)據(jù)庫操作對象
public boolean isPause = false; //是否暫停任務(wù)的標(biāo)識
private int mFinished = 0; //文件下載進(jìn)度
public DownloadTask(Context mContext, FileInfo mFileInfo) {
this.mContext = mContext;
this.mFileInfo = mFileInfo;
mDao = new ThreadDAOImpl(mContext);
}
/**
* 開始下載
*/
public void download(){
this.isPause = false;
//從數(shù)據(jù)庫中通過文件URL獲得下載線程記錄
List<ThreadInfo> threads = mDao.getThreadInfos(mFileInfo.getUrl());
ThreadInfo threadInfo = null;
if(threads.size() > 0){
//讀取數(shù)據(jù)庫中下載線程記錄,因為是單線程下載所以get(0)
threadInfo = threads.get(0);
}else{
//如果數(shù)據(jù)庫中沒有記錄,創(chuàng)建新的線程信息記錄
threadInfo = new ThreadInfo(0,mFileInfo.getUrl(),0,mFileInfo.getLength(),0);
//插入下載線程信息
mDao.insertThreadInfo(threadInfo);
}
//啟動線程進(jìn)行下載
DownloadThread thread = new DownloadThread(threadInfo);
thread.start();
}
/**
* 數(shù)據(jù)下載線程
*/
class DownloadThread extends Thread{
private ThreadInfo mThreadInfo = null; //線程下載記錄
public DownloadThread(ThreadInfo threadInfo){
mThreadInfo = threadInfo;
}
public void run(){
HttpURLConnection conn = null;
RandomAccessFile raf = null;
InputStream input = null;
try {
// //插入下載線程信息
// if(!mDao.isExists(mThreadInfo.getUrl(), mThreadInfo.getId())){
// mDao.insertThreadInfo(mThreadInfo);
// }
//打開連接
URL url = new URL(mThreadInfo.getUrl());
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setRequestMethod("GET");
//設(shè)置下載位置
int start = mThreadInfo.getStart()+mThreadInfo.getFinished();
conn.setRequestProperty("Range", "bytes="+start+"-"+mThreadInfo.getEnd());
//設(shè)置本地文件寫入位置
File file = new File(DownloadService.DOWNLOAD_DIR, mFileInfo.getName());
raf = new RandomAccessFile(file,"rwd");
raf.seek(start);
//累加完成進(jìn)度
mFinished += mThreadInfo.getFinished();
if(conn.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT){
//下載文件
int len = 0;
byte[] buffer = new byte[1024];
Intent intent = new Intent(DownloadService.ACTION_UPDATE);
long time = System.currentTimeMillis();
input = conn.getInputStream();
//讀取文件中的數(shù)據(jù)
while((len = input.read(buffer))!=-1){
//寫入本地文件
raf.write(buffer,0,len);
//累加整個文件完成進(jìn)度
mFinished += len;
//間隔500毫秒更新一次進(jìn)度,減少UI更新頻率
if(System.currentTimeMillis() - time > 500){
time = System.currentTimeMillis();
//發(fā)送進(jìn)度到Activity
intent.putExtra("finished", mFinished * 100 / mFileInfo.getLength());
mContext.sendBroadcast(intent);
}
//下載任務(wù)暫停,結(jié)束下載
if(isPause){
//保存進(jìn)度到數(shù)據(jù)庫
mDao.updateThreadInfo(mThreadInfo.getId(),?
mThreadInfo.getUrl(),?
mFinished);
return;
}
}
}
//刪除下載記錄
mDao.deleteThreadInfo(mThreadInfo.getUrl());
//提示下載完成
Intent intent = new Intent(DownloadService.ACTION_FINISH);
mContext.sendBroadcast(intent);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
conn.disconnect();
raf.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
2015-05-15
補(bǔ)充下 ,我發(fā)現(xiàn)是connection.setRequestMethod("GET"); 出現(xiàn)exception了,說java.net.ProtocolException: Connection already established。 可是職能openConnection之后才能拿到connection啊 ,求問 怎么破啊