Kotlin,减少重复代码
每个my API服务接口类都有创建静态方法Kotlin,减少重复代码,kotlin,Kotlin,每个my API服务接口类都有创建静态方法 interface AuthApiService { @FormUrlEncoded @POST("api/auth/login") fun postLogin(@Field("username") username: String, @Field("password") password: String): io.reactivex.Observable<LoginApiResponse>
interface AuthApiService {
@FormUrlEncoded
@POST("api/auth/login")
fun postLogin(@Field("username") username: String, @Field("password") password: String):
io.reactivex.Observable<LoginApiResponse>
companion object Factory {
fun create(): AuthApiService {
val gson = GsonBuilder().setLenient().create()
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("http:192.168.24.188:8080")
.build()
return retrofit.create(AuthApiService::class.java)
}
}
}
interface BBBApiService {
companion object Factory {
fun create(): BBBApiService {
val gson = GsonBuilder().setLenient().create()
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("http:192.168.24.188:8080")
.build()
return retrofit.create(BBBApiService::class.java)
}
}
}
接口验证服务{
@FormUrlEncoded
@POST(“api/auth/login”)
趣味postLogin(@Field(“用户名”)用户名:字符串,@Field(“密码”)密码:字符串):
可观察的
伴星工厂{
fun create():AuthApiService{
val gson=GsonBuilder().setLenient().create()
val reformation=reformation.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(“http:192.168.24.188:8080”)
.build()
返回reformation.create(AuthApiService::class.java)
}
}
}
接口设备{
伴星工厂{
乐趣创建():BBBApiService{
val gson=GsonBuilder().setLenient().create()
val reformation=reformation.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(“http:192.168.24.188:8080”)
.build()
返回reformation.create(BBBApiService::class.java)
}
}
}
但是,我只想定义create()方法一次
所以我上了蜂房课
interface ApiFactory {
companion object {
inline fun <reified T>createRetrofit(): T {
val gson = GsonBuilder().setLenient().create()
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("http://192.168.24.188:8080")
.build()
return retrofit.create(T::class.java)
}
}
}
interface AuthApiService {
@FormUrlEncoded
@POST("api/auth/login")
fun postLogin(@Field("username") username: String, @Field("password") password: String):
io.reactivex.Observable<LoginApiResponse>
companion object Factory {
fun create(): AuthApiService {
return ApiFactory.createRetrofit()
}
}
接口工厂{
伴星{
inline fun CreateReformation():T{
val gson=GsonBuilder().setLenient().create()
val reformation=reformation.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(“http://192.168.24.188:8080")
.build()
return reformation.create(T::class.java)
}
}
}
接口身份验证服务{
@FormUrlEncoded
@POST(“api/auth/login”)
趣味postLogin(@Field(“用户名”)用户名:字符串,@Field(“密码”)密码:字符串):
可观察的
伴星工厂{
fun create():AuthApiService{
返回ApiFactory.createReformation()
}
}
但是,我仍然需要在AuthApiService中定义create()方法
有没有办法将ApiFactory类实现为SubApi类,这样我就不必在每个子类中定义create方法?您可以像这样修改
ApiFactory
:
interface ApiFactory {
companion object {
inline fun <reified T>createRetrofit(klass: KClass<T>): T {
val gson = GsonBuilder().setLenient().create()
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("http://192.168.24.188:8080")
.build()
return retrofit.create(klass.java)
}
}
}
您可以这样修改您的
ApiFactory
:
interface ApiFactory {
companion object {
inline fun <reified T>createRetrofit(klass: KClass<T>): T {
val gson = GsonBuilder().setLenient().create()
val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("http://192.168.24.188:8080")
.build()
return retrofit.create(klass.java)
}
}
}
一个简单的解决方案就是直接调用
ApiFactory
的函数:
val authApiService = ApiFactory.createRetrofit<AuthApiService>()
然后创建一个可与ApiFactoryClient一起使用的扩展函数:
一个简单的解决方案就是直接调用
ApiFactory
的函数:
val authApiService = ApiFactory.createRetrofit<AuthApiService>()
然后创建一个可与ApiFactoryClient一起使用的扩展函数:
我认为没有必要更改签名。为什么不简单地将函数调用为
val authApiService=ApiFactory.createRefundation()
?@hotkey你是对的,这应该可以很好地工作,我只是KotlinI的新手,我认为没有必要更改签名。为什么不简单地将函数调用为val authApiService=apifacture.createRefundation()
?@hotkey你是对的,这应该可以很好地工作,我只是Kotlin的新手
inline fun <reified T> ApiFactoryClient<T>.create(): T = ApiFactory.createRetrofit<T>()
val authApiService = AuthApiService.create()