I can not connect to my service using retrofit2 using API 19

3

The problem I have is that I was testing with different versions of the SDK and with a samsung SM-A500M version of the API 19 it jumps in front of the onFailure and the error that I get is the following:

  

SSL handshake aborted: ssl = 0x78cb85f0: I / O error during system call, Connection reset by peer

I have tried API 22 more and if you connected without problems.

The class I used is this

class UnsafeOkHttpClient {
companion object {
fun getUnsafeOkHttpClient(): OkHttpClient.Builder {
    try {
        // Create a trust manager that does not validate certificate chains
        val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
            @Throws(CertificateException::class)
            override fun checkClientTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
            }

            @Throws(CertificateException::class)
            override fun checkServerTrusted(chain: Array<java.security.cert.X509Certificate>, authType: String) {
            }

            override fun getAcceptedIssuers(): Array<java.security.cert.X509Certificate> {
                return arrayOf()
            }
        })

        val sslContext = SSLContext.getInstance("SSL")
        sslContext.init(null, trustAllCerts, java.security.SecureRandom())

        val sslSocketFactory = sslContext.socketFactory

        val builder = OkHttpClient.Builder()
        builder.sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
        builder.hostnameVerifier { _, _ -> true }

        return builder
    } catch (e: Exception) {
        throw RuntimeException(e)
    }
}
}
}

and the way I use it is

val okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient()

val retrofit = Retrofit.Builder()
        .baseUrl(Constants.RUTA_API)
        .client(okHttpClient.build())
        .addConverterFactory(GsonConverterFactory.create())
        .build()

I've also tried using the 'com.squareup.okhttp3:okhttp:3.9.1' library, but I get the same error.

    
asked by Sebastian Peredo Murga 22.02.2018 в 20:15
source

1 answer

2

The device you mention has Android 4.4.4 (KitKat), I remember this problem in Android 4.4, which is related to the cryptographic protocol that you use when supporting SSL .

It is important to note what the documentation indicates for the use of SSLContext :

  

Each implementation of the Java platform is required to support > following standard SSLContext protocol: TLSv1

In fact the one that should be used now is TLS since it is the successor of SSL , but in the case of Android the following information is important :

For API 19 you can use SSLv3 or TLSv1 being recommended TLSv1 .

 val sslContext = SSLContext.getInstance("TLSv1")
    
answered by 22.02.2018 / 21:24
source