【问题标题】:Kotlinx Serialization @Body converter error while using HashMap with Any typeKotlinx 序列化 @Body 转换器在使用任何类型的 HashMap 时出错
【发布时间】:2020-10-30 13:40:06
【问题描述】:

我正在使用Kotlinx SerializationRetrofit 并尝试通过HashMapBody 发送参数,如下所示

@POST("post")
suspend fun sendPost(@Body params : HasMap<String, @Contextual @RawValue Any>) : GenericResponse<Post>

但它给出了这个错误信息

Unable to create @Body converter for java.util.HashMap<java.lang.String, java.lang.Object> (parameter #1)

我还将SerializersModule 添加到Retrofit,但没有帮助

fun provideRetrofit(baseUrl : String, okHttpClient: OkHttpClient) : Retrofit {
    val contentType = "application/json".toMediaType()
    val json = Json {
        serializersModule = SerializersModule {
            contextual(String.serializer())
            contextual(Int.serializer())
            contextual(Double.serializer())
            contextual(Boolean.serializer())
        }
    }
    return Retrofit.Builder()
        .baseUrl(baseUrl)
        .client(okHttpClient)
        .addConverterFactory(json.asConverterFactory(contentType))
        .build()
}

有人可以帮忙吗?

Kotlinx Version 1.0.1

Kotlinx Converter Version 0.8.0

Kotlin Version 1.4.10

【问题讨论】:

    标签: android kotlin kotlinx.serialization


    【解决方案1】:

    根据this issue,我们无法做到这一点

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 1970-01-01
      • 2019-06-17
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      • 2014-01-09
      • 1970-01-01
      相关资源
      最近更新 更多