Skip to content

Instantly share code, notes, and snippets.

@axiel7
Last active August 12, 2021 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axiel7/ababf580b0b6a48df6d114fb0ef17de7 to your computer and use it in GitHub Desktop.
Save axiel7/ababf580b0b6a48df6d114fb0ef17de7 to your computer and use it in GitHub Desktop.
Ktor HttpClient for Android
import android.util.Log
import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.features.logging.*
import java.util.concurrent.TimeUnit
object KtorClient {
private const val TIME_OUT = 60_000L
val ktorHttpClient = HttpClient(OkHttp) {
//evitar excepcion al recibir error del servidor
expectSuccess = false
engine {
config {
connectTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
callTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
readTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
writeTimeout(TIME_OUT, TimeUnit.MILLISECONDS)
retryOnConnectionFailure(true)
}
}
//TODO: desactivar en produccion
install(Logging) {
logger = object : Logger {
override fun log(message: String) {
Log.v("Ktor =>", message)
}
}
level = LogLevel.ALL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment