下载简历不适用于setRequestProperty方法

时间:2014-08-30 16:14:46

标签: android parse-error android-download-manager resume-download

这是我的doInBackground方法:

@Override
    protected String doInBackground(String... sUrl) {

        InputStream input = null;

        HttpURLConnection conection = null;

        BufferedOutputStream bout = null;
        FileOutputStream fos = null;


        int downloaded = 0;


        try {
            URL url = new URL(sUrl[0]);
            conection = (HttpURLConnection)url.openConnection();
            int lenghtOfFile = conection.getContentLength();

            if(STATUS) {
                File file = new File(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk");
                if (file.exists()) {
                    downloaded = (int) file.length();
                    conection.setRequestProperty("Range", "bytes=" + (file.length()) + "-");
                }
            }
            else {
                conection.setRequestProperty("Range", "bytes=" + downloaded + "-");
            }
            conection.setDoInput(true);
            conection.setDoOutput(true);

            conection.connect();

            input = new BufferedInputStream(url.openStream(), 8192);
            fos=(downloaded==0)? new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk"): new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/myapp.apk",true);
            bout = new BufferedOutputStream(fos, 1024);
            byte data[] = new byte[1024];

            long total = 0;
            int count = 0;

            while ((count = input.read(data, 0, 1024)) >= 0) {
                if (isCancelled()) {
                    input.close();
                    return null;
                }

                bout.write(data, 0, count);
                downloaded += count;
                publishProgress((int)(downloaded * 100/ lenghtOfFile) );
                total += count;
            }
            bout.flush();
            input.close();
            fos.close();


        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        } finally {
            try {
                if (output != null)
                    output.close();
                if (input != null)
                    input.close();
                if (fos != null)
                    fos.close();
                if (bout != null)
                    bout.close();
            } catch (IOException ignored) {
            }

            if (conection != null)
                conection = null;
        }

        return null;
    }

我使用此代码开始下载任务(resume flag为false - > STATUS = FALSE):

 dt = new DownloadTask(DownloadsActivity.this, false);
 dt.execute("myurl.something.apk"); 

然后当完全下载我启动apk文件,所有的东西都正常工作,apk正确安装。

当我暂停下载时使用此代码:

dt.cancel(true);

然后使用此代码恢复它(resume flag为true-> STATUS = TRUE):

 dt = new DownloadTask(DownloadsActivity.this, true);
 dt.execute("myurl.something.apk"); 

这次apk大小等于last downloaded before pause + apk total size,因此我的apk文件已损坏。
这意味着connection.setRequestProperty()不适合我。

我的代码问题是什么?
提前致谢。

0 个答案:

没有答案