Android IllegalStateException:在调用end

时间:2016-05-19 08:09:56

标签: java android download inputstream httpurlconnection

这是我的Stacktrace:

<StackTrace>java.lang.IllegalStateException: attempt to use Inflater after calling end
    at java.util.zip.Inflater.checkOpen(Inflater.java:332)
    at java.util.zip.Inflater.setInput(Inflater.java:312)
    at com.android.okhttp.okio.InflaterSource.refill(InflaterSource.java:106)
    at com.android.okhttp.okio.InflaterSource.read(InflaterSource.java:62)
    at com.android.okhttp.okio.GzipSource.read(GzipSource.java:80)
    at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:374)
    at java.io.InputStream.read(InputStream.java:162)
    at com.myProgram.services.DownloadService.onHandleIntent(DownloadService.java:58)
    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:158)
    at android.os.HandlerThread.run(HandlerThread.java:61)
</StackTrace>

以下是DownloadService的代码:

 InputStream input = connection.getInputStream();
 byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            Bundle resultData = new Bundle();
            resultData.putInt("progress" ,(int) (total * 100 / fileLength));
            receiver.send(U_PROGRESS, resultData);
            output.write(data, 0, count);
        }

58行是:

while ((count = input.read(data)) != -1) {

在随机情况下会发生此错误。下载服务用于下载应用程序的新版本。

1 个答案:

答案 0 :(得分:0)

请注意,您无法从与下载服务不同的线程(即调用InputStream.read的线程)关闭下载InputStream。

你是否有另一个线程,任何机会尝试使用InputStream.close()api关闭流?这样的行为会导致这种运行时异常。在更新版本的OkHttp中,将从结束线程产生一个类似的异常。请参阅以下内容以获取参考:https://github.com/square/okhttp/issues/1842