Last active
July 16, 2018 13:11
-
-
Save BurakDizlek/11abe3c383bffe47e5fd187aa310de44 to your computer and use it in GitHub Desktop.
Retrofit Settings Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object MyService { | |
private val TIMEOUTOFSECOND = 12 | |
private val _instanceOfService: Service by lazy { setupHttpClient() } | |
fun on(): Service { | |
return _instanceOfService | |
} | |
private fun setupHttpClient(): Service { | |
val cookieManager = CookieManager() | |
CookieHandler.setDefault(cookieManager) // default | |
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL) | |
val client = OkHttpClient.Builder() | |
.connectTimeout(TIMEOUTOFSECOND.toLong(), TimeUnit.SECONDS) | |
.writeTimeout(TIMEOUTOFSECOND.toLong(), TimeUnit.SECONDS) | |
.readTimeout(TIMEOUTOFSECOND.toLong(), TimeUnit.SECONDS) | |
.connectionPool(ConnectionPool()) | |
.build() | |
val gson = GsonBuilder().create() | |
val retrofit = Retrofit.Builder() | |
.baseUrl(Config.BaseUrl) | |
.client(client) | |
.addConverterFactory(GsonStringConverterFactory()) | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.build() | |
return retrofit.create(Service::class.java) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment