Hanbit the Developer
[Kotlin] Implementation Retrofit2 Callback Function 본문
It's simple. Just use some parameters as Unit type, and invoke them.
First, make your custom api calling function as below:
fun <T> enqueueApiCall(
call: Call<T>,
onResponse: (Response<T>)->Unit,
onFailure: ()->Unit
){
call.enqueue(object: Callback<T> {
override fun onResponse(call: Call<T>, response: Response<T>) {
onResponse.innoke()
}
override fun onFailure(call: Call<T>, t: Throwable) {
onFailure.invoke()
}
})
}
And call it in your actual code like:
val call = retrofit.create(YourApi::class.java)
// # Skipped initializing call
enqueueApiCall(
call,
{
Log.d("Test", "onResponse")
},
{
Log.d("Test", "onFailure")
}
)
'Android' 카테고리의 다른 글
[Retrofit2] Video View와 HTTP Range Header에 대해 (0) | 2022.01.11 |
---|---|
[Kotlin] Carousel RecyclerView With PagerSnapHelper (0) | 2022.01.01 |
[Kotlin] RecyclerView in SwipeRefreshLayout in NestedScrollView Implementation (0) | 2021.09.24 |
[Kotlin] Delete whole directory in android (0) | 2021.08.19 |
[Kotlin] Get URI from byte array in android (0) | 2021.08.19 |