Retrofit + Kotlin 请求时报错:Parameter type must not include a type variable or wildcard

使用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)
    }
    */
}
  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值