【发布时间】:2020-04-14 09:46:48
【问题描述】:
我使用Retrofit2连接服务器,
为了简化请求方法的数量,我使用了泛型。但问题是改造不接受泛型方法。我把示例代码放在下面。有谁知道解决办法吗?
照片 1):
import io.reactivex.Observable
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path
interface APIInterface {
@POST("{url}") fun <T> post(
@Path("url") url: String,
@Body body: RequestBody
): Observable<T>
@GET("{url}") fun <T> get(
@Path("url") url: String
): Observable<T>
}
照片 2:
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.RequestBody
import java.io.Serializable
class APIService constructor(private val mApi: APIInterface) {
fun <T: Serializable> post(url: String, body: RequestBody): Observable<T>{
val observable = mApi.post<T>(url, body)
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
return observable
}
fun <T: Serializable> get(url: String): Observable<T> {
val observable = mApi.get<T>(url)
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
return observable
}
}
照片 3:
mAPIService.post<SignUpModel>(URL_SIGN_UP, body)
.subscribe(object : APIObserver<SignUpModel> {
override fun onNext(it: SignUpModel) {
.
.
.
照片 4:
W: java.lang.IllegalArgumentException: Method return type must not include a type variable or wildcard: io.reactivex.Observable<T>
W: for method APIInterface.post
.
.
.
【问题讨论】:
-
将您的代码添加为 text.not screenshots
-
请不要发布图片。 insted 添加代码以便更好地理解
-
你又可以看到了。 @DilanAnuruddha
-
你又可以看到了。 @Parth
-
你可以使用
Observable<Any>
标签: java android generics kotlin retrofit2