Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - How to set connection timeout with OkHttp Kotlin

I see this `

How to set connection timeout with OkHttp

But this link for Java(Android) Language.I want to use kotlin Language... ` I am using OkHttp library

 val client = OkHttpClient()

 val time = client.connectTimeoutMillis() // it's get only methood but i looking for method for set Timeout

and my trouble is I cannot find how to set connection timeout and socket timeout For Kotlin.

like image 841
yahya Avatar asked Dec 14 '22 18:12

yahya


1 Answers

A Builder is required, there are no setters available. With OkHttp 3.9.1 you can do this:

val client = OkHttpClient.Builder()
    .connectTimeout(10, TimeUnit.SECONDS)
    .writeTimeout(10, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build()
like image 54
s1m0nw1 Avatar answered Dec 18 '22 00:12

s1m0nw1