ExceptionInInitializerError with retrofit api request

0

i'm facing little issue with retrofit api request and the issue is taking place in the interface where i set up the configuration for the request , i'm getting this error only in android 4.4 , on higher versions i'm not getting this error , would any please provide some help thank you

  • This is the Api interface
 @GET("everything")
    suspend fun getNews(@Query("q") query: String, @Query("apiKey") apiKey : String
    ,@Query("page")page : Int) : AllNewsModel


    @GET("top-headlines")
    suspend fun getHeadlines(@Query("country")country : String, @Query("apikey")apiKey: String,
    @Query("page") page : Int) : HeadlinesModel


    companion object {
        var response : ApiResponse? = null
        fun News() : ApiResponse {
           response =  Retrofit.Builder()
                .baseUrl(Utils().BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build().create(ApiResponse::class.java)
            return response!!
        }

        fun Headlines() : ApiResponse {
            response =  Retrofit.Builder()
                .baseUrl(Utils().BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build().create(ApiResponse::class.java)
            return response!!
        }
    }

}
  • Two errors in one so first is :
java.lang.ExceptionInInitializerError

  • Second Error is :
Caused by: java.lang.IllegalStateException: Expected Android API level 21+ 
but  was 19 at okhttp3.internal.platform.AndroidPlatform.<clinit>

**Edit : after checking some topics about this issue , i found that a common answer is to add an old version of the OkHttp Library , well i did so but it ended up showing another error , here it is :

error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x75038d5c:0x00000000)
     FATAL EXCEPTION: DefaultDispatcher-worker-2
Process: com.example.testingproject, PID: 10143
javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x78ab22a8: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x75038d5c:0x00000000)
  • This is the old library i added
 implementation("com.squareup.okhttp3:okhttp:3.12.0"){
        force = true 
    }
android
retrofit
asked on Stack Overflow Jun 4, 2020 by taki eddine • edited Jun 4, 2020 by taki eddine

1 Answer

0

After deep searching i found this solution which worked for me on android 4.4 ( api 19 )

  val spec = ConnectionSpec.Builder(ConnectionSpec.COMPATIBLE_TLS)
                .tlsVersions(TlsVersion.TLS_1_2, TlsVersion.TLS_1_1, TlsVersion.TLS_1_0)
                .cipherSuites(
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
                    CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA)
                .build()
            val okttp = OkHttpClient.Builder()
                .connectionSpecs(Collections.singletonList(spec))
           response =  Retrofit.Builder()
                .baseUrl(Utils().BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okttp.build())
                .build().create(ApiResponse::class.java)
answered on Stack Overflow Jun 4, 2020 by taki eddine

User contributions licensed under CC BY-SA 3.0