java - Android Retrofit 2 gzip 错误

标签 java android retrofit retrofit2

我正在使用 retrofit2 开发 android 应用程序。 gzip 有问题,这里是异常堆栈。 我不知道异常到底发生在哪里。任何建议表示赞赏。谢谢。

Unknown exception
java.io.EOFException
   at okio.RealBufferedSource.require(RealBufferedSource.java:64)
   at okio.GzipSource.consumeHeader(GzipSource.java:114)
   at okio.GzipSource.read(GzipSource.java:73)
   at okio.RealBufferedSource.request(RealBufferedSource.java:71)
   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:225)
   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
   at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
   at okhttp3.RealCall.execute(RealCall.java:57)
   at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
   at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.execute(ExecutorCallAdapterFactory.java:89)
   at com.hello.de.aloha.domain.interactor.RetrofitServerInteraction.callVerifyOauthToken(RetrofitServerInteraction.java:424)
   at com.hello.de.aloha.domain.security.SecurityHandshake.validateOAuthToken(SecurityHandshake.java:178)
   at com.hello.de.aloha.domain.security.SecurityHandshake.requestOAuthToken(SecurityHandshake.java:150)
   at com.hello.de.aloha.domain.security.SecurityHandshake.authenticate(SecurityHandshake.java:122)
   at com.hello.de.aloha.domain.security.SecurityHandshake.challenge(SecurityHandshake.java:70)
   at com.hello.de.aloha.domain.interceptor.SecurityInterceptor.determineNewToken(SecurityInterceptor.java:152)
   at com.hello.de.aloha.domain.interceptor.SecurityInterceptor.intercept(SecurityInterceptor.java:89)
   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
   at com.hello.de.aloha.domain.interceptor.SendCookieInterceptor.intercept(SendCookieInterceptor.java:46)
   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
   at com.hello.de.aloha.domain.interceptor.ReceiveCookiesInterceptor.intercept(ReceiveCookiesInterceptor.java:43)
   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
   at com.hello.de.aloha.domain.interceptor.HttpParamInterceptor.intercept(HttpParamInterceptor.java:75)
   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
   at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:203)
   at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:187)
   at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
   at okhttp3.RealCall.access$100(RealCall.java:30)
   at okhttp3.RealCall$AsyncCall.execute(RealCall.java:127)
   at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at java.lang.Thread.run(Thread.java:818)

最佳答案

我检查了 GzipSource 中的代码:

    private void consumeHeader() throws IOException {
        // Read the 10-byte header. We peek at the flags byte first so we know if we
        // need to CRC the entire header. Then we read the magic ID1ID2 sequence.
        // We can skip everything else in the first 10 bytes.
        // +---+---+---+---+---+---+---+---+---+---+
        // |ID1|ID2|CM |FLG|     MTIME     |XFL|OS | (more-->)
        // +---+---+---+---+---+---+---+---+---+---+
        source.require(10);
        ...
    }
  @Override public void require(long byteCount) throws IOException {
    if (!request(byteCount)) throw new EOFException();
  }

  @Override public boolean request(long byteCount) throws IOException {
    if (byteCount < 0) throw new IllegalArgumentException("byteCount < 0: " + byteCount);
    if (closed) throw new IllegalStateException("closed");
    while (buffer.size < byteCount) {
      if (source.read(buffer, Segment.SIZE) == -1) return false;
    }
    return true;
  }

您的响应数据似乎违反了gzip 格式。所以建议先检查响应数据。您可能还需要检查您传入的影响响应数据格式的请求 header (即接受:gzip)。

关于java - Android Retrofit 2 gzip 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737543/

相关文章:

android - Api 调用失败无法为类 Retrofit/Moshi 创建转换器

android - Retrofit 无法创建调用适配器

java - 这段代码中的变量 'x' 会存储在堆栈内存、堆内存还是两者中?

javascript - 向 Nashorn 脚本提供 JavaScript 日期

android - 如何在父 View 中使用 OnClickListener 并在 subview 中使用 onTouchEvent?

Android Sync 适配器一直运行

android - 使用带有 Retrofit 的响应拦截器

java - 使用基于菜单的程序的日期格式和数组

java - 如何避免Eclipse在把类名放在注释的时候引入一个类,让checkstyle后面不报错?

android - 在Android中搜索和播放YouTube视频