在我的 Android 应用程序中,我使用 Retrofit 2:
public enum OperationType {
@SerializedName("payment")
PAYMENT,
@SerializedName("payout")
PAYOUT,
@SerializedName("transfer")
TRANSFER
}
fun getOperationsList(typeList: List<OperationType>, callback: Callback<List<Operation>>) {
val call = myRestClient.getOperationsList(typeList)
executeRequest(call, callback)
}
@GET("/operations")
fun getOperationsList(@Query("type") typeList: List<OperationType>): Call<List<Operation>>
但是我在这一行中遇到运行时错误:
val call = myRestClient.getOperationsList(typeList)
错误:
Shutting down VM
FATAL EXCEPTION: main
Process: md.qsystems.android.tango.debug, PID: 22714
java.lang.IllegalArgumentException: Parameter type must not include a type variable or wildcard: java.util.List<? extends OperationType> (parameter #1)
for method TangoRestClient.getOperationsList
at retrofit2.Utils.methodError(Utils.java:52)
at retrofit2.Utils.methodError(Utils.java:42)
at retrofit2.Utils.parameterError(Utils.java:61)
at retrofit2.RequestFactory$Builder.validateResolvableType(RequestFactory.java:764)
at retrofit2.RequestFactory$Builder.parseParameterAnnotation(RequestFactory.java:401)
at retrofit2.RequestFactory$Builder.parseParameter(RequestFactory.java:306)
at retrofit2.RequestFactory$Builder.build(RequestFactory.java:193)
at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:67)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:26)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
at retrofit2.Retrofit$1.invoke(Retrofit.java:149)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy1.getOperationsList(Unknown Source)
at mTransportService.getOperationsList(TransportService.kt:160)
您可以在 String 类型之前或 List 之前的尖括号内添加
。 两者都应该有效并删除通配符。@JvmSuppressWildcards
取自此问题的解决方法。
如果请求中有多个
List
,您可能应该添加几个 @JvmSuppressWildcards
。
@GET("/get-teams")
suspend fun teams(
@Query("status") status: List<@JvmSuppressWildcards TeamStatus>? = null,
@Query("enabled") enabled: Boolean? = null,
@Query("type") type: List<@JvmSuppressWildcards TeamType>? = null,
@Query("name") name: String? = null,
): List<Team>