Created
November 4, 2018 06:37
-
-
Save imrankst1221/d53d41c5c8cb8db4528db1185e65b8b8 to your computer and use it in GitHub Desktop.
Create OkHttpClient worker
This file contains 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 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