title: SSLHandshakeException: Connection closed by peer on android app
body: <p>I'm developing an app for my company, that needs to post some data on an <strong>https</strong> url.</p>
  
  <p>I'm using retro fit, but I'm getting the following error <code>SSLHandshakeException: Connection closed by peer</code></p>
  
  <p>this is the code I'm using</p>
  
  <p><strong>Service Generator</strong></p>
  
  <pre><code>public class ServiceGenerator {
  
      public static final String API_BASE_URL = "https://website.com/api/";
  
      private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
  
      private static Retrofit.Builder builder =
              new Retrofit.Builder()
                      .baseUrl(API_BASE_URL)
                      .addConverterFactory(GsonConverterFactory.create());
  
      public static &lt;S&gt; S createService(Class&lt;S&gt; serviceClass) {
          Retrofit retrofit = builder.client(httpClient.build()).build();
          return retrofit.create(serviceClass);
      }
  }
  </code></pre>
  
  <p><strong>Service</strong></p>
  
  <pre><code>public interface ITrackTraceService {
  
      @Headers("Content-Type: application/json")
      @POST("beepbeep/postdata")
      Call&lt;String&gt; PostLocation(@Body DataInput input);
  
  }
  </code></pre>
  
  <p><strong>Call itself</strong></p>
  
  <pre><code>Location location = getLocation();
  
          TrackTraceService.ITrackTraceService srv = ServiceGenerator.createService(TrackTraceService.ITrackTraceService.class);
  
          DataInput i = new DataInput();
          i.Eta = new Date();
          i.Reference = "123456789";
          i.Status = "Tracking";
          i.Device = new Device();
          i.Reading = new Reading();
          i.Reading.Time = new Date();
          i.Reading.Location = new DeviceLocation();
          i.Reading.Location.Accuracy = location.getAccuracy();
          i.Reading.Location.Altitude = (float) location.getAltitude();
          i.Reading.Location.Latitude = (float) location.getLatitude();
          i.Reading.Location.Longitude = (float) location.getLongitude();
          i.Reading.Location.Speed = (float) location.getSpeed();
          i.Reading.Location.Time = new Date(location.getTime());
          Call&lt;String&gt; call = srv.PostLocation(i);
  
          call.enqueue(new Callback&lt;String&gt;() {
              @Override
              public void onResponse(Call&lt;String&gt; call, Response&lt;String&gt; response) {
  
                  if (response.isSuccessful()) {
                      Log.d("Service", "onResponse");
                  } else {
                      // error response, no access to resource?
                  }
              }
  
              @Override
              public void onFailure(Call&lt;String&gt; call, Throwable throwable) {
                  StringWriter sw = new StringWriter();
                  PrintWriter pw = new PrintWriter(sw);
                  throwable.printStackTrace(pw);
                  Log.d("Service", call.request().toString());
                  Log.e("Service", "onFailure", throwable);
                  Log.d("Error", throwable.getMessage());
              }
          });
      }
  </code></pre>
  
  <p><strong>versions</strong></p>
  
  <pre><code>compile 'com.android.support:appcompat-v7:23.4.0'
  compile 'com.android.support:design:23.4.0'
  compile files('libs/converter-gson-2.0.2.jar')
  compile files('libs/okhttp-3.3.1.jar')
  compile files('libs/gson-2.6.2.jar')
  compile files('libs/okio-1.8.0.jar')
  compile files('libs/retrofit-2.0.2.jar')
  compile files('libs/logging-interceptor-3.3.1.jar')
  building for android API version 23
  </code></pre>
  
  <p><strong>stacktrace</strong></p>
  
  <pre><code>javax.net.ssl.SSLHandshakeException: Connection closed by peer
      at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
      at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:318)
      at okhttp3.internal.io.RealConnection.connectTls(RealConnection.java:239)
      at okhttp3.internal.io.RealConnection.establishProtocol(RealConnection.java:196)
      at okhttp3.internal.io.RealConnection.buildConnection(RealConnection.java:171)
      at okhttp3.internal.io.RealConnection.connect(RealConnection.java:111)
      at okhttp3.internal.http.StreamAllocation.findConnection(StreamAllocation.java:187)
      at okhttp3.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:123)
      at okhttp3.internal.http.StreamAllocation.newStream(StreamAllocation.java:93)
      at okhttp3.internal.http.HttpEngine.connect(HttpEngine.java:296)
      at okhttp3.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
      at okhttp3.RealCall.getResponse(RealCall.java:243)
      at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
      at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
      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)
      Suppressed: javax.net.ssl.SSLHandshakeException: Connection closed by peer
  </code></pre>