【问题标题】:java.net.ProtocolException: unexpected end of stream, AsyncTask File Download failed on slow networksjava.net.ProtocolException:流意外结束,AsyncTask 文件下载在慢速网络上失败
【发布时间】:2016-01-08 14:14:25
【问题描述】:

我正在使用 AsyncTask 下载音频文件。文件正在高速互联网(如 wifi)上下载,但应用程序在低速互联网(如移动数据)上的流结束时出现错误。 我的下载文件代码是:

`URLConnection  conexion = urle.openConnection();
conexion.connect();
InputStream input = new BufferedInputStream(conexion.getInputStream());
totalSize = conexion.getContentLength();
OutputStream output = new FileOutputStream(st);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) 
{
  output.write(data, 0, count);
  downloadedSize += count;
}                               
output.flush();
output.close();
input.close();`

以下异常即将到来:

`java.net.ProtocolException: unexpected end of stream
        at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.close(HttpConnection.java:326)
        at com.android.okio.RealBufferedSink.close(RealBufferedSink.java:174)
        at com.android.okio.RealBufferedSink$1.close(RealBufferedSink.java:142)`

help

从此链接错误是由于(通常设置在响应的内容长度标头中)大于响应中的实际数据。但是怎么解决???

【问题讨论】:

    标签: java android android-asynctask


    【解决方案1】:

    我遇到了类似的问题。最好使用Download Manager

    DownloadManager 会在网络发生变化时自动尝试下载,并且适用于慢速互联网连接。

    【讨论】:

    • 感谢您的友好回答。但是在大约 70% 之后的下载过程中出现了这个错误:“不幸的是进程 android.process.media 已停止”????
    • 这是由于网络不稳定。所以有时候网速很慢,应用下载不到响应。
    猜你喜欢
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 2011-03-16
    相关资源
    最近更新 更多