使用Retrofit + Kotlin请求接口时,遇到问题,报错日志:
Parameter type must not include a type variable or wildcard: java.util.Map<java.lang.String, ?> (parameter #1)
for method NetworkRequestService.getLotteryNumber
代码如下:
interface NetworkRequestService {
@Headers("content-type: application/x-www-form-urlencoded; charset=UTF-8")
@POST("henei5/api_tool/trend")
@FormUrlEncoded
fun getLotteryNumber(@FieldMap map: Map<String, Any>): Call<ResponseBody>
/*
参数提供示例:
// 提交的数据: period=20&date=2020-08-09&filter=0&pos=1
val map = hashMapOf<String, Any>().apply {
put("period", 200)
put("date", "2020-08-09")
put("filter", 0)
put("pos", 1)
}
*/
}
问题出在参数map的value类型Any.对于java来说,这个value的类型是Object,可以被Retrofit识别,但对于kotlin来说,retrofit会把Any识别成 ?,就报出了错误.
解决办法:
在Any类型前面添加注解@JvmSuppressWildcards
interface NetworkRequestService {
@Headers("content-type: application/x-www-form-urlencoded; charset=UTF-8")
@POST("henei5/api_tool/trend")
@FormUrlEncoded
fun getLotteryNumber(@FieldMap map: Map<String, @JvmSuppressWildcards Any>): Call<ResponseBody>
/*
参数提供示例:
// 提交的数据: period=20&date=2020-08-09&filter=0&pos=1
val map = hashMapOf<String, Any>().apply {
put("period", 200)
put("date", "2020-08-09")
put("filter", 0)
put("pos", 1)
}
*/
}