Reputation: 425
i'm currently working on some basic app which i try to get an response from API - List of objects.
My data classes Are:
@JsonClass(generateAdapter = true)
data class Tag(
@Json(name = "id")
val id: Int,
@Json(name = "name")
val name: String
)
@JsonClass(generateAdapter = true)
data class Test(
@Json(name = "count")
val count: Int,
@Json(name = "next")
val next: Int,
@Json(name = "previous")
val previous: Int,
@Json(name = "results")
val results: List<Tag>
)
My retrofit build code is:
val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
return Retrofit.Builder()
.baseUrl(SERVER_BASE_URL)
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.addConverterFactory(MoshiConverterFactory.create(moshi))
.build()
And my request is very simple:
@GET("api/tags")
suspend fun getTags(): Deferred<Test>
But when i am calling getTags()
i get the following error:
java.lang.IllegalArgumentException: Unable to create converter for kotlinx.coroutines.Deferred<com.example.kotlin_ex2.models.Test>
Caused by: java.lang.IllegalArgumentException: No JsonAdapter for kotlinx.coroutines.Deferred<com.example.kotlin_ex2.models.Test> (with no annotations)
Already tried many other ways with no success, what could be the problem?
Thank you
Upvotes: 2
Views: 1845
Reputation: 2342
It is because you are using both suspend
and Deferred
in one function. Convert
@GET("api/tags")
suspend fun getTags(): Deferred<Test>
to
@GET("api/tags")
fun getTags(): Deferred<Test>
Upvotes: 5
Reputation: 425
Solved. As i can see, i just needed to remove the Deferred class and write it that way:
@GET("api/tags")
suspend fun getTags(): Test
and no need to call await()
from inside the Coroutine.
Upvotes: 0