Skip to content

Instantly share code, notes, and snippets.

@imrankst1221
Created November 4, 2018 06:37
Show Gist options
  • Save imrankst1221/d53d41c5c8cb8db4528db1185e65b8b8 to your computer and use it in GitHub Desktop.
Save imrankst1221/d53d41c5c8cb8db4528db1185e65b8b8 to your computer and use it in GitHub Desktop.
Create OkHttpClient worker
object ApiWorker {
private var mClient: OkHttpClient? = null
private var mGsonConverter: GsonConverterFactory? = null
/**
* Don't forget to remove Interceptors (or change Logging Level to NONE)
* in production! Otherwise people will be able to see your request and response on Log Cat.
*/
val client: OkHttpClient
@Throws(NoSuchAlgorithmException::class, KeyManagementException::class)
get() {
if (mClient == null) {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
val httpBuilder = OkHttpClient.Builder()
httpBuilder
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.addInterceptor(interceptor) /// show all JSON in logCat
mClient = httpBuilder.build()
}
return mClient!!
}
val gsonConverter: GsonConverterFactory
get() {
if(mGsonConverter == null){
mGsonConverter = GsonConverterFactory
.create(GsonBuilder()
.setLenient()
.disableHtmlEscaping()
.create())
}
return mGsonConverter!!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment