Skip to content

Instantly share code, notes, and snippets.

@BurakDizlek
Last active July 16, 2018 13:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BurakDizlek/11abe3c383bffe47e5fd187aa310de44 to your computer and use it in GitHub Desktop.
Save BurakDizlek/11abe3c383bffe47e5fd187aa310de44 to your computer and use it in GitHub Desktop.
Retrofit Settings Sample
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