title: Okhttp java.net.ProtocolException: unexpected end of stream
body: <p>im currently working on Android aplication and im making a simple GET request using okttp3. Im using my flask aplication as the API endpoint.
  <strong>Code Android</strong>:</p>
  
  <pre><code> OkHttpClient client = new OkHttpClient();
  
          final Request request = new Request.Builder()
                  .url(url)
                  .build();
  
          client.newCall(request).enqueue(new Callback() {
              @Override
              public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  
                      String s = response.body().string();
                      System.out.println(s);
  
              }
  
              @Override
              public void onFailure(@NotNull Call call, @NotNull IOException e) {
  
                  System.out.println(e.getMessage());
              }
  
          });
  </code></pre>
  
  <p><strong>Code Flask</strong></p>
  
  <pre><code>@app.route('/abc', methods=["GET"])
  def abc():
      return jsonify({"test": "abc"})
  </code></pre>
  
  <p>When i make the request using POSTMAN i have no issues:
  <a href="https://i.stack.imgur.com/joNXe.png" rel="nofollow noreferrer">postman get request</a>
  However when using the java code above sometimes it works, other times i get the following error</p>
  
  <pre><code>D/OkHttp: java.net.ProtocolException: unexpected end of stream
          at okhttp3.internal.http1.Http1ExchangeCodec$FixedLengthSource.read(Http1ExchangeCodec.kt:389)
          at okhttp3.internal.connection.Exchange$ResponseBodySource.read(Exchange.kt:276)
          at okio.Buffer.writeAll(Buffer.kt:1655)
          at okio.RealBufferedSource.readString(RealBufferedSource.kt:95)
          at okhttp3.ResponseBody.string(ResponseBody.kt:187)
          at com.example.teste.MainActivity$2.onResponse(MainActivity.java:83)
          at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:504)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
  D/OkHttp:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
          at java.lang.Thread.run(Thread.java:764)
  </code></pre>
  
  <p>I have tried mutiple solutions that have been sugested at other posts like:</p>
  
  <pre><code>OkHttpClient client = new OkHttpClient.Builder()
      .retryOnConnectionFailure(true)
      .build();
  
  </code></pre>
  
  <p>or </p>
  
  <pre><code>Request request = new Request.Builder()
                      .addHeader("Authorization", "Bearer " + apiToken)
                      .addHeader("content-type", "application/json")
                      .addHeader("Connection","close")
                      .url(uri)
                      .build();
  </code></pre>
  
  <p>but none of them work. Whats the problem here? I have tried making requests with the same code with other dummy api and it always works. What is my flask api missing?</p>
  
  <p>The error happens here:</p>
  
  <pre><code>val read = super.read(sink, minOf(bytesRemaining, byteCount))
        if (read == -1L) {
          connection.noNewExchanges() // The server didn't supply the promised content length.
          val e = ProtocolException("unexpected end of stream")
          responseBodyComplete()
          throw e
        }
  </code></pre>
  
  <p>It has something to do with the content lenght supplied by the server. How do i solve this?</p>