Skip to content

Instantly share code, notes, and snippets.

@valterh4ck3r
Created April 18, 2018 01:30
Show Gist options
  • Save valterh4ck3r/7e4d38df35c9fba339d89ab5232c3909 to your computer and use it in GitHub Desktop.
Save valterh4ck3r/7e4d38df35c9fba339d89ab5232c3909 to your computer and use it in GitHub Desktop.
Retrofit Guide Implemetation
interface ExampleAPI {
@POST("example")
fun example(@Body checkModel : CheckModel) : Call<CheckModel>
}
class ExampleClient {
companion object {
private val httpClient = OkHttpClient
.Builder()
.connectTimeout(3 , TimeUnit.MINUTES)
.readTimeout(3 , TimeUnit.MINUTES)
.build()
private val builderV1 = Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
fun <TService> createClientV1(serviceClass: Class<TService>): TService {
val retrofit = builderV1.client(httpClient).build()
return retrofit.create(serviceClass)
}
}
}
fun methodExample(activity: Activity , successAction: Runnable , errorAction: Runnable){
doAsync {
val exampleRequest = ExampleClient.createClientV1(ExampleAPI::class.java)
.example(checkModel)
.execute()
activity.runOnUiThread{
if(exampleRequest.isSuccessful){
successAction.run()
} else{
errorAction.run()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment