引发java.net.ProtocolException:流的意外结束

时间:2018-06-29 06:26:33

标签: android android-studio kotlin retrofit

我尝试使用Android 8(26 Oreo)从服务器中获取数据,但出现以下错误:

java.net.ProtocolException: unexpected end of stream
    at 
okhttp3.internal.http1.Http1Codec$FixedLengthSource.read(Http1Codec.java:398)
    at okio.RealBufferedSource.read(RealBufferedSource.java:46)
    at okio.ForwardingSource.read(ForwardingSource.java:35)
    at 
retrofit2.OkHttpCall$ExceptionCatchingRequestBody$1.read(OkHttpCall.java:291)
    at okio.RealBufferedSource$1.read(RealBufferedSource.java:430)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:287)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:350)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:179)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at okhttp3.ResponseBody$BomAwareReader.read(ResponseBody.java:252)
    at com.google.gson.stream.JsonReader.fillBuffer(JsonReader.java:1287)
    at 
  com.google.gson.stream.JsonReader.nextNonWhitespace(JsonReader.java:1325)
    at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:482)
    at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:414)
    at 
  com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read
 (Reflecti veTypeAdapterFactory.java:214)
     at 
 retrofit2.converter.gson.GsonResponseBodyConverter.convert
  (GsonResponseBodyConve rter.java:37)
    at 
 retrofit2.converter.gson.GsonResponseBodyConverter.convert
(GsonResponseBodyConve rter.java:25)
    at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:112)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at 
  java.util.concurrent.ThreadPoolExecutor.runWorker
 (ThreadPoolExecutor.java:1133)
    at 
  java.util.concurrent.ThreadPoolExecutor$Worker.run
   (ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)

我正在使用Retrofit 2.3.0和OkHttp 3.9.0。奇怪的是,此错误仅在API 26 Oreo上发生,而不在以前的任何API上发生,我是随机获得此错误的,有时是,有时没有。

3 个答案:

答案 0 :(得分:0)

您可能要检查提供的解决方案here

答案 1 :(得分:0)

在您的改造电话上尝试使用此@Header("Connection","close")。它为我解决了问题。

答案 2 :(得分:0)

如果您的应用在模拟器中,请在您的手机上试用。这对我有用。