title: OkHttp post streaming - switching from cellular to WiFi
body: <p>I'm using <code>OkHttp</code> to stream files up to the server. My code looks something like this:</p>
  
  <pre><code>OkHttpClient client = new OkHttpClient.builder()
          .connectTimeout(20, TimeUnit.SECONDS)
          .readTimeout(20, TimeUnit.SECONDS)
          .writeTimeout(20, TimeUnit.SECONDS)
          .retryOnConnectionFailure(false)
          .build();
  
  Response response = null;
  try {
          RequestBody requestBody = new RequestBody() {
              private long uploadedBytes;
  
              @Override
              public MediaType contentType() {
                  // return type based on file
              }
  
              @Override
              public void writeTo(BufferedSink sink) throws IOException {
                  byte[] buffer = new byte[8 * 1024];
                  int count = 0;
                  while ((count = source.read(buffer)) != -1) {
                      sink.write(buffer, 0, count);
                  }
                  source.close();
              }
          }
  
          final Request request = new Request.Builder()
                      .url(uploadUrl)
                      .put(requestBody)
                      // header stuff
                      .build();
  
          response = client.newCall(request).execute();
  } catch (IOException e) {
      // handle
  }
  </code></pre>
  
  <p>In my code I'm streaming files (bytes) and my timeout set on the <code>OkHttpClient</code> is 20 seconds.</p>
  
  <p>So, this is my scenario - a user is uploading a file on LTE when the phone connects to a WiFi spot. Right now what happens is that OkHttp continues to upload for some time (which I think is the timeout value but I'm not too sure), then fails with a broken pipe.</p>
  
  <pre><code>javax.net.ssl.SSLException: Write error: ssl=0x74a6315680: I/O error during system call, Broken pipe
      at com.android.org.conscrypt.NativeCrypto.SSL_write(Native Method)
      at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:824)
      at okio.Okio$1.write(Okio.java:78)
      ...
  </code></pre>
  
  <p>At that point the task is rescheduled and picked up on WiFi whenever <code>JobScheduler</code> feels like it. So all in all, the switch between LTE and WiFi becomes quite painful and takes up to 30 seconds on a good day (timeout plus scheduler). </p>
  
  <p>Considering that the reverse scenario works perfectly fine (WiFi -> LTE), I was hoping maybe there's something in <code>OkHttp</code> that I could use to improve this transition? My version of <code>OkHttp</code> is 3.4.1</p>