2

i am using the following:

 implementation "com.squareup.retrofit2:converter-gson:2.6.0"

fun getOkHTTPClient(time: Int, interceptor: Interceptor): OkHttpClient {

        val client = OkHttpClient.Builder()
            .addInterceptor(interceptor)
           .connectTimeout(5, TimeUnit.SECONDS)
            .writeTimeout(5, TimeUnit.SECONDS)
            .readTimeout(5, TimeUnit.SECONDS)
            .callTimeout(5, TimeUnit.SECONDS)
            .retryOnConnectionFailure(false)
        return client.build()
    }

retrofit2.Retrofit.Builder()
                .baseUrl("$url/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)

If i change the timeouts to 10 seconds, 20, 50, 500 etc.... the effect is the same. the call timeout is not obeying these rules.

Am i missing something?

Note: i am using Asynchronous requests if it helps in debugging the case

Rashad.Z
  • 2,494
  • 2
  • 27
  • 58
  • Do you mean that the connection gets interrupted before a timeout? – Sergei Voitovich Jul 30 '19 at 08:59
  • No, the connection is not getting interrupted while it should be since its taking more than the set amount of seconds – Rashad.Z Jul 30 '19 at 09:00
  • does your interceptor manipulate the timeouts? If not you could try to add the timeouts in the interceptor, just to see if it's working. – mmi Jul 30 '19 at 09:44
  • @mmi how do i add the timeouts in the interceptor? i only add some headers in it – Rashad.Z Jul 30 '19 at 09:58
  • @rashad.z you can call `.withConnectTimeout(timeout, unit)` etc. on the chain before calling `.proceed()` [see this issue](https://github.com/square/retrofit/issues/2561). But you are using the most recent OkHttp(3.12.0) & Retrofit(2.5.0), right? – mmi Jul 30 '19 at 10:01
  • just tried it and also with no luck .addInterceptor { chain -> chain.withConnectTimeout(5, TimeUnit.SECONDS) chain.withReadTimeout(5, TimeUnit.SECONDS) chain.withWriteTimeout(5, TimeUnit.SECONDS) chain.proceed( chain.request().newBuilder() .header("Accept", StaticVars.JSON_HEADER_REQUEST) .header("Authorization", Security.getEncryptedPass()) .build() ) – Rashad.Z Jul 30 '19 at 10:03
  • But you are using the most recent OkHttp(3.12.0) & Retrofit(2.5.0), right? – mmi Jul 30 '19 at 10:12
  • @mmi yes i am using the following implementation implementation "com.squareup.retrofit2:retrofit:2.6.0" implementation "com.squareup.retrofit2:converter-gson:2.6.0" implementation 'com.squareup.okhttp3:okhttp:3.12.0' – Rashad.Z Jul 30 '19 at 10:21
  • 1
    Please check this answer, it worked for me. https://stackoverflow.com/a/62746271/1164529 You may just only need to comment out the callTimeout method there. – Harpreet Jan 05 '22 at 13:18

0 Answers0