title: Unexpected end of stream error on second hit to API
body: <p>I am trying Okhttp3 library and I am new to it. I read some of the tutorials and its working fine until this use case pops up:</p>
  
  <p>I have an external Asynctask class where I am executing my POST and GET codes. So when I call the Asynctask for the first time it works fine. The second time I call the same Asynctask its giving me the error java.io.IOException: unexpected end of stream on okhttp3.Address.....</p>
  
  <p>Can someone please explain why is this happening? I am creating the OkHttpClient object in the activity and passing it to the Asynctask.</p>
  
  <p>This is the constructor:</p>
  
  <pre><code>public Post(JsonObject jsonParams){
  this.jsonParams = jsonParams;
  }
  </code></pre>
  
  <p>This is the background task:</p>
  
  <pre><code>Response response = null;
      try {
          OkHttpClient client = new OkHttpClient.Builder()
                  .retryOnConnectionFailure(true)
                  .build();
  
          RequestBody body = RequestBody.create(JSON, jsonParams);
          Request.Builder requestBuilder = new Request.Builder()
                  .url(url)
                  .post(body)
                  .addHeader(APP_AUTHORIZATION_KEY, AUTHORIZATION_KEY);
  
          Request request = requestBuilder.build();
  
          response = client.newCall(request).execute();
          if (response.isSuccessful()) {
              responseBody = response.body().string();
              Log.v("POST RESPONSE", "response" + responseBody);
          }
      } catch (Exception ex) {
          ex.printStackTrace();
      } finally {
          if (response != null)
              response.close();
      }
  </code></pre>
  
  <p>I am posting my log:</p>
  
  <pre><code>java.io.IOException: unexpected end of stream on okhttp3.Address@d8a95c17
  12-02 10:30:11.544 20214-22391/com.test.network W/System.err:     at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:199)
  12-02 10:30:11.544 20214-22391/com.test.network W/System.err:     at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
  12-02 10:30:11.544 20214-22391/com.test.network W/System.err:     at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:775)
  12-02 10:30:11.544 20214-22391/com.test.network W/System.err:     at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:86)
  12-02 10:30:11.544 20214-22391/com.test.network W/System.err:     at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:760)
  12-02 10:30:11.544 20214-22391/com.test.network W/System.err:     at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:613)
  12-02 10:30:11.544 20214-22391/com.test.network W/System.err:     at okhttp3.RealCall.getResponse(RealCall.java:244)
  12-02 10:30:11.544 20214-22391/com.test.network W/System.err:     at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
  12-02 10:30:11.564 20214-22391/com.test.network W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
  12-02 10:30:11.574 20214-22391/com.test.network W/System.err:     at okhttp3.RealCall.execute(RealCall.java:57)
  12-02 10:30:11.574 20214-22391/com.test.network W/System.err:     at com.test.network.tasks.Post.doInBackground(Post.java:60)
  12-02 10:30:11.574 20214-22391/com.test.network W/System.err:     at com.test.network.tasks.Post.doInBackground(Post.java:21)
  12-02 10:30:11.584 20214-22391/com.test.network W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
  12-02 10:30:11.584 20214-22391/com.test.network W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  12-02 10:30:11.584 20214-22391/com.test.network W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  12-02 10:30:11.584 20214-22391/com.test.network W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  12-02 10:30:11.584 20214-22391/com.test.network W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  12-02 10:30:11.604 20214-22391/com.test.network W/System.err:     at java.lang.Thread.run(Thread.java:841)
  12-02 10:30:11.604 20214-22391/com.test.network W/System.err: Caused by: java.io.EOFException: \n not found: size=0 content=…
  12-02 10:30:11.614 20214-22391/com.test.network W/System.err:     at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:215)
  12-02 10:30:11.614 20214-22391/com.test.network W/System.err:     at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
  </code></pre>