我获取文件的AsyncTask安卓下载长度为-1 [英] i get -1 length of file asynctask android download

查看:120
本文介绍了我获取文件的AsyncTask安卓下载长度为-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我得-1作为文件的长度。我尝试了很多的时间来修复它。我不知道为什么会这样。此外,当我再次调用AsyncTask的,松1保持不变(但松1值都没有任何问题,我测试了一个烤面包和它的作品)。了松1'是一个字符串,每次具有不同的值。谢谢你。

  @覆盖
保护对话框onCreateDialog(INT ID){
    开关(ID){
        案例DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog =新ProgressDialog(本);            mProgressDialog.setMessage(下载+松1 +);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(假);
            mProgressDialog.show();
            返回mProgressDialog;
        默认:
            返回null;
    }
}
保护字符串doInBackground(字符串... aurl){
        诠释计数;        尝试{
            网址URL =新的URL(aurl [0]);
            URLConnection的conexion = url.openConnection();
            ((HttpURLConnection类)conexion).setInstanceFollowRedirects(真);
            conexion.connect();
            INT lenghtOfFile = conexion.getContentLength();
            Log.d(ANDRO_ASYNC,Lenght文件:+ lenghtOfFile);            InputStream的输入=新的BufferedInputStream(url.openStream());
            OutputStream的输出=新的FileOutputStream(/ SD卡/下载/+松1);            字节的数据[] =新的字节[20480]            总长= 0;
            而((计数= input.read(数据))!= - 1){
                总+ =计数;
                publishProgress(+(int)的((总/ lenghtOfFile)* 100));
                output.write(数据0,计);
            }            output.flush();
            output.close();
            input.close();
        }赶上(例外五){}
        返回null;    }

和这里是我的LogCat中

  20 03-08:25:17.714:D / ANDRO_ASYNC(14559):文件长度:-1
03-08 20:25:18.564:D / dalvikvm(14559):GC释放18632对象/ 1179128字节112ms
03-08 20:25:18.864:I /全球(14559):默认缓冲区的BufferedInputStream构造用大小。它会更好,如果需要一个8K缓冲器是明确的。
03-08 20:25:18.874:D / ANDRO_ASYNC(14559):-420000
03-08 20:25:18.954:D / ANDRO_ASYNC(14559):-700000
03-08 20:25:18.984:D / ANDRO_ASYNC(14559)-1820000
03-08 20:25:19.029:D / ANDRO_ASYNC(14559)-1960000


解决方案

获得-1的长度并不少见。通常,这意味着服务器发送它的数据,你(可能是因为它已满)的缓冲区,但它仍然不知道响应的整体尺寸。或者,这是一个懒惰的开发者。

随着-1的长度,你只需要保持加载信息,直到你在流的末尾。

I have a problem i get -1 as length of the file. I tried a lot of time to fix it. I don't know why this happens. Also when i call again the asynctask , song1 stays the same(but the song1 value have not any problem, i tested with a toast and it works ). the 'song1' is a string and each time have different values. Thank you.

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);

            mProgressDialog.setMessage("Downloading "+song1+"..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
    }
}




protected String doInBackground(String... aurl) {
        int count;

        try {
            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            ((HttpURLConnection) conexion).setInstanceFollowRedirects(true);
            conexion.connect();


            int lenghtOfFile = conexion.getContentLength();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);

            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/download/"+song1);

            byte data[] = new byte[20480];

            long total = 0;


            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress(""+(int)((total/lenghtOfFile)*100));
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {}
        return null;

    }

and here is my LogCat

03-08 20:25:17.714: D/ANDRO_ASYNC(14559): Lenght of file: -1
03-08 20:25:18.564: D/dalvikvm(14559): GC freed 18632 objects / 1179128 bytes in 112ms
03-08 20:25:18.864: I/global(14559): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
03-08 20:25:18.874: D/ANDRO_ASYNC(14559): -420000
03-08 20:25:18.954: D/ANDRO_ASYNC(14559): -700000
03-08 20:25:18.984: D/ANDRO_ASYNC(14559): -1820000
03-08 20:25:19.029: D/ANDRO_ASYNC(14559): -1960000

解决方案

Getting a length of -1 isn't uncommon. Usually that means that the server is sending it's buffer of data to you (likely because it is full), but it still does not know the entire size of the response. Or it's a lazy developer.

With a length of -1, you just need to keep loading information until you are at the end of the stream.

这篇关于我获取文件的AsyncTask安卓下载长度为-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆