LMaker
LMaker

Reputation: 1630

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?

Upvotes: 4

Views: 5025

Answers (1)

maciekjanusz
maciekjanusz

Reputation: 4775

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.

Upvotes: 12

Related Questions