Created
April 18, 2018 01:30
-
-
Save valterh4ck3r/7e4d38df35c9fba339d89ab5232c3909 to your computer and use it in GitHub Desktop.
Retrofit Guide Implemetation
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
interface ExampleAPI { | |
@POST("example") | |
fun example(@Body checkModel : CheckModel) : Call<CheckModel> | |
} |
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
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) | |
} | |
} | |
} |
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
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