title: Retrofit : java.lang.IllegalStateException: closed
body: <p>I am using two kind of interceptor, one is <strong>HttpLoggingInterceptor</strong> and another one is my custom <strong>AuthorizationInterceptor</strong></p>
  
  <p>I am using below updated retrofit version library,</p>
  
  <pre><code>def retrofit_version = "2.7.2"
  implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
  implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
  implementation 'com.squareup.okhttp3:logging-interceptor:4.4.0'
  implementation 'com.squareup.okhttp3:okhttp:4.4.0'
  </code></pre>
  
  <p>below is code</p>
  
  <pre><code>private fun makeOkHttpClient(): OkHttpClient {
          val logger = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
          return OkHttpClient.Builder()
              .addInterceptor(AuthorizationInterceptor(context)) &lt;---- To put Authorization Barrier
              .addInterceptor(logger) &lt;---- To log Http request and response
              .followRedirects(false)
              .connectTimeout(50, TimeUnit.SECONDS)
              .readTimeout(50, TimeUnit.SECONDS)
              .writeTimeout(50, TimeUnit.SECONDS)
              .build()
      }
  </code></pre>
  
  <p>When I try to execute below code, in file named SynchronizationManager.kt, it gives me an error.</p>
  
  <pre><code>var rulesResourcesServices = RetrofitInstance(context).buildService(RulesResourcesServices::class.java)
  val response = rulesResourcesServices.getConfigFile(file).execute() &lt;---In this line I am getting an exception... (which is at SynchronizationManager.kt:185)               
  </code></pre>
  
  <p>My RulesResourcesServices class is here</p>
  
  <p>After debug I found that when below function called, at that time I am getting an exception</p>
  
  <pre><code>@GET("users/me/configfile")
      fun getConfigFile(@Query("type") type: String): Call&lt;ResponseBody&gt;
  </code></pre>
  
  <p>I am getting following error</p>
  
  <pre><code>java.lang.IllegalStateException: closed
  at okio.RealBufferedSource.read(RealBufferedSource.kt:184)
  at okio.ForwardingSource.read(ForwardingSource.kt:29)
  at retrofit2.OkHttpCall$ExceptionCatchingResponseBody$1.read(OkHttpCall.java:288)
  at okio.RealBufferedSource.readAll(RealBufferedSource.kt:293)
  at retrofit2.Utils.buffer(Utils.java:316)&lt;------- ANDROID IS HIGH-LIGHTING
  at retrofit2.BuiltInConverters$BufferingResponseBodyConverter.convert(BuiltInConverters.java:103)
  at retrofit2.BuiltInConverters$BufferingResponseBodyConverter.convert(BuiltInConverters.java:96)
  at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:225)
  at retrofit2.OkHttpCall.execute(OkHttpCall.java:188)
  at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall.execute(DefaultCallAdapterFactory.java:97)
  at android.onetap.SynchronizationManager.downloadFile(SynchronizationManager.kt:185)
  at android.base.repository.LoginRepository.downloadConfigFilesAndLocalLogin(LoginRepository.kt:349)
  at android.base.repository.LoginRepository.access$downloadConfigFilesAndLocalLogin(LoginRepository.kt:48)
  at android.base.repository.LoginRepository$loginTask$2.onSRPLoginComplete(LoginRepository.kt:210)
  at android.base.repository.LoginRepository$performSyncLogin$srpLogin$1$1.onSRPLogin(LoginRepository.kt:478)
  at android.srp.SRPManager$SRPLoginOperation$execute$1.invokeSuspend(SRPManager.kt:323)
  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
  at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:561)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:727)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:667)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:655)
  </code></pre>
  
  <p>Below is screenshot, in that you can see that, I am getting output of file but don't know why it is throwing an exception. </p>
  
  <p><a href="https://i.stack.imgur.com/fmyQH.png" rel="noreferrer"><img src="https://i.stack.imgur.com/fmyQH.png" alt="enter image description here"></a></p>
  
  <p>checked Retrofit's Utils class</p>
  
  <p><a href="https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Utils.java" rel="noreferrer">https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Utils.java</a></p>
  
  <pre><code>static ResponseBody buffer(final ResponseBody body) throws IOException {
      Buffer buffer = new Buffer();
      body.source().readAll(buffer); &lt;-This line throws an error.
      return ResponseBody.create(body.contentType(), body.contentLength(), buffer);
    }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Same thing is working fine with enqueue method.</p>
  
  <pre><code>response.enqueue(object : Callback&lt;ResponseBody?&gt; {
  
  override fun onResponse(call: Call&lt;ResponseBody?&gt;, response: retrofit2.Response&lt;ResponseBody?&gt;) { 
   }
  })
  </code></pre>
  
  <p>I have post same issue with Retrofit team, lets see.</p>
  
  <p><a href="https://github.com/square/retrofit/issues/3336" rel="noreferrer">https://github.com/square/retrofit/issues/3336</a></p>