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
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>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.