Android IllegalStateException:调用 end 后尝试使用 Infflat



这是我的堆栈跟踪:

<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>

这是来自下载服务的代码:

 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) {

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

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

您是否有另一个线程尝试使用 InputStream.close() API 关闭流? 此类行为将导致此类运行时异常。在较新版本的 OkHttp 上,关闭线程将产生类似的异常。请参阅下面的参考: https://github.com/square/okhttp/issues/1842

最新更新