Skip to content

Instantly share code, notes, and snippets.

@kobeumut
Forked from BurakDizlek/MyServiceClass.kt
Last active August 25, 2017 08:48
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 kobeumut/46d7c897a0ac43cae5262a4505fb13f9 to your computer and use it in GitHub Desktop.
Save kobeumut/46d7c897a0ac43cae5262a4505fb13f9 to your computer and use it in GitHub Desktop.
Retrofit Settings Sample
object MyService {
private val TIMEOUTOFSECOND = 15
private val _instanceOfService: Service by lazy { setupHttpClient() }
fun on(): Service {
return _instanceOfService
}
private fun setupHttpClient(): Service {
val cookieManager = CookieManager()
CookieHandler.setDefault(cookieManager)
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