使用Retrofit + Kotlin请求接口时,遇到问题,报错日志:
Parameter type must not include a type variable or wildcard: java.util.Map<java.lang.String, ?> (parameter #1)
代码大致如下:
//参数
val map : Map<String,Any> = hashMapOf(
//添加参数
"time" to System.CurrentTimeMillis
)
//接口定义
@FormUrlEncoded
@POST("api/box-mgmt")
fun regBox(@FieldMap map: Map<String, Any>): Observable<BaseResponseBean<Any>>
问题出在参数map的value类型Any.对于java来说,这个value的类型是Object,可以被Retrofit识别,但对于kotlin来说,retrofit会把Any识别成 ?,就报出了错误.
解决办法:
添加注解@JvmSuppressWildcards
@FormUrlEncoded
@POST("api/box-mgmt")
fun regBox(@FieldMap map: Map<String, Any>):@JvmSuppressWildcards Observable<BaseResponseBean<Any>>
本文来自:Segmentfault
感谢作者:idealcn
在使用Retrofit + Kotlin进行接口请求时遇到一个问题,错误信息显示:Parameter type must not include a type variable or wildcard。错误源于Kotlin中Any类型的参数被Retrofit解析为?导致。解决方法是在参数上添加特定注解。
5812





