为了账号安全,请及时绑定邮箱和手机立即绑定

下载完安装的时候出现“解析包时出现问题”

请问下,为什么我下载下来(apk文件)的文件大小都是对的,也关闭了。但是安装的时候总是“解析包时出现问题”没法安装呢


@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();

}

}

}


正在回答

2 回答

可以参考下:

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;

/**

 * 下载任务类

 *

 */

public class DownloadTask {

private Context mContext = null; //程序上下文

private FileInfo mFileInfo = null; //下载文件的信息

private ThreadDAO mDao = null; //数据库操作对象

public boolean isPause = false; //是否暂停任务的标识

private int mFinished = 0; //文件下载进度

public DownloadTask(Context mContext, FileInfo mFileInfo) {

this.mContext = mContext;

this.mFileInfo = mFileInfo;

mDao = new ThreadDAOImpl(mContext);

}

/**

* 开始下载

*/

public void download(){

this.isPause = false;

//从数据库中通过文件URL获得下载线程记录

List<ThreadInfo> threads = mDao.getThreadInfos(mFileInfo.getUrl());

ThreadInfo threadInfo = null;

if(threads.size() > 0){

//读取数据库中下载线程记录,因为是单线程下载所以get(0)

threadInfo = threads.get(0);

}else{

//如果数据库中没有记录,创建新的线程信息记录

threadInfo = new ThreadInfo(0,mFileInfo.getUrl(),0,mFileInfo.getLength(),0);

//插入下载线程信息

mDao.insertThreadInfo(threadInfo);

}

//启动线程进行下载

DownloadThread thread = new DownloadThread(threadInfo);

thread.start();

}

/**

* 数据下载线程

*/

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");

//设置下载位置

int start = mThreadInfo.getStart()+mThreadInfo.getFinished();

conn.setRequestProperty("Range", "bytes="+start+"-"+mThreadInfo.getEnd());

//设置本地文件写入位置

File file = new File(DownloadService.DOWNLOAD_DIR, mFileInfo.getName());

raf = new RandomAccessFile(file,"rwd");

raf.seek(start);

//累加完成进度

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();

//读取文件中的数据

while((len = input.read(buffer))!=-1){

//写入本地文件

raf.write(buffer,0,len);

//累加整个文件完成进度

mFinished += len;

//间隔500毫秒更新一次进度,减少UI更新频率

if(System.currentTimeMillis() - time > 500){

time = System.currentTimeMillis();

//发送进度到Activity

intent.putExtra("finished", mFinished * 100 / mFileInfo.getLength());

mContext.sendBroadcast(intent);

}

//下载任务暂停,结束下载

if(isPause){

//保存进度到数据库

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();

}

}

}

}

}


0 回复 有任何疑惑可以回复我~
#1

小微妮与阿暖 提问者

没用的 connection.setRequestMethod("GET")的时候还是会出现java.net.ProtocolException: Connection already established
2015-05-23 回复 有任何疑惑可以回复我~

补充下 ,我发现是connection.setRequestMethod("GET"); 出现exception了,说java.net.ProtocolException: Connection already established。 可是职能openConnection之后才能拿到connection啊 ,求问 怎么破啊

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android-Service系列之断点续传下载
  • 参与学习       20437    人
  • 解答问题       87    个

想升职加薪么?本章课程你值得拥有,满满的干货,学起来吧

进入课程

下载完安装的时候出现“解析包时出现问题”

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信