Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create @Body converter for class in Retrofit

I have an instance of retrofit built like this

val moshi = Moshi.Builder()
        .add(SkipBadElementsListAdapter.Factory)
        .add(KotlinJsonAdapterFactory())
        .add(Date::class.java, MoshiDateAdapter())
        .build()

val okHttpClient = createHttpClientBuilder()
        .build()

return Retrofit.Builder()
        .client(okHttpClient)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(MoshiConverterFactory.create(moshi))
        .baseUrl(Interactors.apiEndpoint)
        .build()
        .create(UserApiClient::class.java)

I'm sending an List of this object

internal open class QuizAnswerDto(
        @Json(name = "questionOrder") val questionOrder: Int?,
        @Json(name = "questionKind") val type: String?,
        @Json(name = "questionId") val questionId: String?,
        @Json(name = "response") val response: Any?,
        @Json(name = "order") val answerOrder: Int?,
        @Json(name = "text") val answerText: String?,
        @Json(name = "responses") val answersMap: Map<Int, String>?){
    companion object {
        const val ANGRY_ID = 0
        const val UPSET_ID = 1
        const val NEUTRAL_ID = 2
        const val SATISFIED_ID = 3
        const val HAPPY_ID = 4
        const val UNKNOWN = -1

        const val LIKE_DISLIKE= "yes_no"
        const val SENTIMENT ="viewer_sentiment"
        const val SINGLE_ANSWER="multiple_choice"
        const val MULTIPLE_ANSWERS="select_all_that_apply"
        const val SHORT_ANSWER="short_answer"
    }
}

With this API call

@POST("campaigns/influencer/sponsorships/watchandrespond/{influencerSponsorshipId}/answers")

@JvmSuppressWildcards
fun submitAnswers(@Path("influencerSponsorshipId") influencerSponsorshipId: String,
                      @Body request: List<QuizAnswerDto>): Completable

When I do, I get this error:

java.lang.IllegalArgumentException: Unable to create @Body converter for java.util.List<com.weare8.android.data.quiz.QuizAnswerDto> (parameter #2) Caused by: java.lang.IllegalArgumentException: No JsonAdapter for E (with no annotations)

Parameter #2 (questionKind) is always one of the const strings in the companion object, I have no idea what "type variable or wildcard" it is talking about. What am I doing wrong?

like image 845
andrewedgar Avatar asked Oct 25 '25 19:10

andrewedgar


1 Answers

From the moshi documentation

val moshi = Moshi.Builder()
    .addLast(KotlinJsonAdapterFactory())
    .build()

KotlinJsonAdapterFactory should be added as last in the builder. This may solve your problem.

like image 167
Jameido Avatar answered Oct 27 '25 08:10

Jameido



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!