Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
–
–
–
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()
Not much different than the accepted answer, but it seems it is best to return the same OkHttpClient to avoid memory leaks.
sealed class ClientBuilder {
companion object {
val plainClient: OkHttpClient by lazy {
OkHttpClient
.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(3, TimeUnit.SECONDS)
.writeTimeout(3, TimeUnit.SECONDS)
.build()
fun client() : OkHttpClient {
return plainClient
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.