title: Android pre-lollipop devices giving error "SSL handshake aborted: ssl=0x618d9c18: I/O error during system call, Connection reset by peer"
body: <p>Iam having this strange issue in which the retrofit keeps throwing me </p>
  
  <blockquote>
    <p>"SSL handshake aborted: ssl=0x618d9c18: I/O error during system call,
    Connection reset by peer"</p>
  </blockquote>
  
  <p>in kitkat, whereas the same code working fine in lollipop devices. Iam using an OkHttpClient client like the following</p>
  
  <pre><code>public OkHttpClient getUnsafeOkHttpClient() {
      try {
          final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
              @Override
              public void checkClientTrusted(
                      java.security.cert.X509Certificate[] chain,
                      String authType) {
              }
              @Override
              public void checkServerTrusted(
                      java.security.cert.X509Certificate[] chain,
                      String authType) {
              }
              @Override
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                  return new java.security.cert.X509Certificate[0];
              }
          } };
  
          int cacheSize = 10 * 1024 * 1024; // 10 MB
          Cache cache = new Cache(getCacheDir(), cacheSize);
          final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
          sslContext.init(null, trustAllCerts,
                  new java.security.SecureRandom());
          final SSLSocketFactory sslSocketFactory = sslContext
                  .getSocketFactory();
          OkHttpClient okHttpClient = new OkHttpClient();
          okHttpClient = okHttpClient.newBuilder()
                  .cache(cache)
                  .sslSocketFactory(sslSocketFactory)
                  .hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).build();
          return okHttpClient;
      } catch (Exception e) {
          throw new RuntimeException(e);
      }
  
  }
  </code></pre>
  
  <p>Iam using this client in retrofit like this</p>
  
  <pre><code>Retrofit retrofit = new Retrofit.Builder()
              .baseUrl(URL)
              .client(getUnsafeOkHttpClient())
              .addConverterFactory(GsonConverterFactory.create())
              .build();
  </code></pre>
  
  <p>EDIT : adding the <code>getUnsafeOkHttpClient()</code> has no effect here and it is not at all recommended to bypass the ssl check by using <code>getUnsafeOkHttpClient()</code></p>
  
  <p>FYI : The issue was because the api endpoint supports only <strong><code>TLS 1.2</code></strong> which was disabled by default on android devices <strong><code>16&lt;device&lt;20</code></strong> . So for <strong><code>16&lt;device&lt;20</code></strong>, create a custom <code>SSLSocketFactory</code></p>