Gingerbread 上的 Android Inputstream.read 问题(下载时)

发布于 2024-11-25 15:42:39 字数 3611 浏览 0 评论 0原文

我在这里没有找到这样的问题。

昨天,我终于在 Nexus One 上安装了 Gingerbread 2.3.4。当我再次打开应用程序(基本上将 XML Feed 加载到 ListView 中)时,它在下载时卡住了。

看来是InputStream流; ->流.read(缓冲区);完成后不再返回-1。

代码与这里几乎相同 下载进度

这是我的代码

public InputStream getInputStreamFromURL(String urlString, DownloadProgressCallback callback) 
    throws IOException, IllegalArgumentException
    {
        InputStream in = null;

        conn = (HttpURLConnection) new URL(urlString).openConnection();
        fileSize = conn.getContentLength();
        out = new ByteArrayOutputStream((int) fileSize);
        conn.connect();

        stream = conn.getInputStream();
        // loop with step 1kb
        while (status == DOWNLOADING) {
            byte buffer[];

            if (fileSize - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[(int) (fileSize - downloaded)];
            }
            int read = stream.read(buffer);

            if (read == -1) {
                break;
            }
            // writing to buffer
            out.write(buffer, 0, read);
            downloaded += read;
            // update progress bar
            callback.progressUpdate((int) ((downloaded / fileSize) * 100));
        }// end of while

        if (status == DOWNLOADING) {
            status = COMPLETE;
        }
        in= (InputStream) new ByteArrayInputStream(out.toByteArray());
        // end of class DownloadImageTask()
        return in;     
    }

:问题基本上是当下载完成时,stream.read(buffer) 返回 0 而不是 -1。当我更改

if (read == -1) {
            break;
        }

为 0 或

if (fileSize == downloaded) {
            break;
        }

在 MainActivity 上收到 ParseExceptions (ExpatParser) 时。 在 2.2 上它运行得非常完美。

我清除了应用程序缓存并尝试了其他一些操作,但我现在真的陷入困境。

我希望有人能帮助我。 :)

更新:

太棒了,你就是那个人,纪尧姆。 :) 非常感谢你,这挽救了我的夜晚! :)

你的代码满足我的需要:

public InputStream getStreamFromURL(String urlString, DownloadProgressCallback callback){
    // initialize some timeouts
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters,3000);

    // create the connection
    URL url;
    try {
        url = new URL(urlString);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
     // connection accepted
        if(httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            int size = connection.getContentLength();

            int index = 0;
            int current = 0;



                InputStream input = connection.getInputStream();
                BufferedInputStream buffer = new BufferedInputStream(input);
                byte[] bBuffer = new byte[1024];
                out = new ByteArrayOutputStream((int) size);

                while((current = buffer.read(bBuffer)) != -1) {

                    out.write(bBuffer, 0, current);
                    index += current;
                    callback.progressUpdate((index/size)*100);
                }
                out.close();

        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return (InputStream) new ByteArrayInputStream(out.toByteArray());

}

I didn't find any question like this here.

Yesterday I finally got Gingerbread 2.3.4 on my Nexus One. When I opened my application (basically loads an XML Feed into a ListView) again, it got stuck while downloading.

It seems that InputStream stream; -> stream.read(buffer); doesn't return -1 any more, when it's finished.

The Code ist nearly the same from here Download Progress

Here's my code:

public InputStream getInputStreamFromURL(String urlString, DownloadProgressCallback callback) 
    throws IOException, IllegalArgumentException
    {
        InputStream in = null;

        conn = (HttpURLConnection) new URL(urlString).openConnection();
        fileSize = conn.getContentLength();
        out = new ByteArrayOutputStream((int) fileSize);
        conn.connect();

        stream = conn.getInputStream();
        // loop with step 1kb
        while (status == DOWNLOADING) {
            byte buffer[];

            if (fileSize - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[(int) (fileSize - downloaded)];
            }
            int read = stream.read(buffer);

            if (read == -1) {
                break;
            }
            // writing to buffer
            out.write(buffer, 0, read);
            downloaded += read;
            // update progress bar
            callback.progressUpdate((int) ((downloaded / fileSize) * 100));
        }// end of while

        if (status == DOWNLOADING) {
            status = COMPLETE;
        }
        in= (InputStream) new ByteArrayInputStream(out.toByteArray());
        // end of class DownloadImageTask()
        return in;     
    }

The problem basically is that when the download finishes, stream.read(buffer) returns 0 instead of -1. When I change

if (read == -1) {
            break;
        }

to 0 or

if (fileSize == downloaded) {
            break;
        }

I get ParseExceptions (ExpatParser) on my MainActivity.
On 2.2 it runs really perfect.

I cleared the app cache and tried a few other things already, but I'm really stuck now.

I hope that someone can help me. :)

UPDATE:

That's awesome, you're the man, Guillaume. :)
Thank you very much, that saved my evening! :)

Your Code for my needs here:

public InputStream getStreamFromURL(String urlString, DownloadProgressCallback callback){
    // initialize some timeouts
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters,3000);

    // create the connection
    URL url;
    try {
        url = new URL(urlString);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
     // connection accepted
        if(httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            int size = connection.getContentLength();

            int index = 0;
            int current = 0;



                InputStream input = connection.getInputStream();
                BufferedInputStream buffer = new BufferedInputStream(input);
                byte[] bBuffer = new byte[1024];
                out = new ByteArrayOutputStream((int) size);

                while((current = buffer.read(bBuffer)) != -1) {

                    out.write(bBuffer, 0, current);
                    index += current;
                    callback.progressUpdate((index/size)*100);
                }
                out.close();

        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return (InputStream) new ByteArrayInputStream(out.toByteArray());

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2024-12-02 15:42:39

此代码适用于我的 2.3.4 Nexus One :

try {
    // initialize some timeouts
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);

    // create the connection
    URL url = new URL(toDownload);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection) connection;

    // connection accepted
    if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        try {
            file = new File(destination);
            // delete the file if exists
            file.delete();
        } catch (Exception e) {
            // nothing
        }

        int size = connection.getContentLength();

        int index = 0;
        int current = 0;

        try {
            file = new File(destination);
            file.delete();
            FileOutputStream output = new FileOutputStream(file);
            InputStream input = connection.getInputStream();
            BufferedInputStream buffer = new BufferedInputStream(input);
            byte[] bBuffer = new byte[10240];

            while ((current = buffer.read(bBuffer)) != -1) {
                if (isCancelled()) {
                    file.delete();
                    break;
                }

                try {
                    output.write(bBuffer, 0, current);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                index += current;
                publishProgress(index / (size / 100));
            }
            output.close();
        } catch (SecurityException se) {
            se.printStackTrace();
            return 1;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }

        return 0;
    }

    // connection refused
    return 2;
} catch (IOException e) {
    return 2;
}

This code work on my 2.3.4 Nexus One :

try {
    // initialize some timeouts
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);

    // create the connection
    URL url = new URL(toDownload);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection) connection;

    // connection accepted
    if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        try {
            file = new File(destination);
            // delete the file if exists
            file.delete();
        } catch (Exception e) {
            // nothing
        }

        int size = connection.getContentLength();

        int index = 0;
        int current = 0;

        try {
            file = new File(destination);
            file.delete();
            FileOutputStream output = new FileOutputStream(file);
            InputStream input = connection.getInputStream();
            BufferedInputStream buffer = new BufferedInputStream(input);
            byte[] bBuffer = new byte[10240];

            while ((current = buffer.read(bBuffer)) != -1) {
                if (isCancelled()) {
                    file.delete();
                    break;
                }

                try {
                    output.write(bBuffer, 0, current);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                index += current;
                publishProgress(index / (size / 100));
            }
            output.close();
        } catch (SecurityException se) {
            se.printStackTrace();
            return 1;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }

        return 0;
    }

    // connection refused
    return 2;
} catch (IOException e) {
    return 2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文