在其中一个类中,调用了另一个类RequestInterface的方法
val requestInterface = RequestInterface.getRetrofitBuild(ExampleApi::class.java)
返回所需对象的getRetrofitBuild方法在哪里。
fun getRetrofitBuild(exampleApi : ExampleApi): ExampleApi {
return Retrofit.Builder()
.baseUrl(baseDomain)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build().create(exampleApi::class)
}
我怎样才能做到这一点,以便在这种方法中不仅可以覆盖ExampleApi,还可以覆盖其他对象,例如SimpleApi?
interface ExampleApi {
@GET("getItems?")
fun getAllItems(@Query("type") type: String): Observable<List<Items>> }
interface SimpleApi {
@GET("getItems?")
fun getAllItems(@Query("lang") language: String): Observable<List<Items>>
}
泛型会帮助你。速写:
该方法返回一个 retorfit 服务,而不是一个构建器。这就是我重命名它的原因。