简体   繁体   English

改造泛型类型调用方法

[英]Retrofit generic type call method

Is it possible to create generic type of calls for example例如,是否可以创建通用类型的调用

@POST("/service/")
fun<T> startRequest(@Body loginReq: Any): Call<T>

then just call it like this然后就这样称呼它

val request = api.startRequest<MyModel_1>(loginReq)

when I write like this and run the method, it says:当我这样写并运行该方法时,它说:

java.lang.IllegalArgumentException: Method return type must not include a type variable or wildcard: retrofit2.Call<T>

尝试使用@JvmSuppressWildcards注释函数

Retrofit(2.6.3) needs to know the types at compile time, when the annotation is being processed. Retrofit(2.6.3) 需要在编译时知道类型,当注解被处理时。 So I don't think it's possible at all to use a generic if Retrofit won't support it in the future.因此,如果 Retrofit 将来不支持它,我认为根本不可能使用泛型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM