Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - Retrofit call void using Kotlin

I'm with a problem with Retrofit 2. I want to use the Call<Void> to make a call without handle the response body, but it doesnt work with Kotlin.

What I need to use instead of Void?

like image 581
LMaker Avatar asked Dec 14 '22 19:12

LMaker


1 Answers

I want to use the Call to make a call without handle the response body, but it doesnt work with Kotlin

That is not true, it does work.

Something different must be wrong with your setup, or your test case is ill defined if you don't get the expected result.

A very simple example:

interface GitHub {

    @GET("/users/{username}/repos") 
    fun getUserRepos(@Path("username") username: String): Call<Void>
}

val github = Retrofit.Builder()
        .baseUrl("https://api.github.com/")
        .build()
        .create(GitHub::class.java)

github.getUserRepos("maciekjanusz")
        .enqueue(object : Callback<Void> {
            override fun onFailure(call: Call<Void>?, t: Throwable?) {
                // failure
            }

            override fun onResponse(call: Call<Void>?, response: Response<Void>?) {
                // success
            }
        })

I tried the above snippets in an Android example, using Kotlin 1.1.61 and Retrofit 2.3.0 and it works correctly - the call is executed and depending on the network availability and the overall setup, the correct Callback method is called.

like image 71
maciekjanusz Avatar answered Dec 31 '22 13:12

maciekjanusz