GithubLink 说个题外话:买个实物工具要钱,弄个软件工具都是免费的!给个星鼓励下嘛!java
Android项目若是是多服务端接口时,通常怎么弄呢?react
把服务器地址(或地址名,如:‘SERVER_HOST_A’)放在接口Header中,而后经过拦截器来动态修改请求地址而实现的。除了默认服务器的接口,其它都要加一个Header,有点麻烦。看起来也不爽,不简洁。android
interface ApiHeaderCase {
/************************** server A ****************************/
@Headers("host:$SERVER_HOST_A")
@GET("user/loginWithScanCode")
fun aMethod1(@Query("id") id: Int): Observable<ResponseBody>
/************************** server B ****************************/
@Headers("host:$SERVER_HOST_B")
@GET("user/loginWithScanCode")
fun bMethod1(@Query("id") id: Int): Observable<ResponseBody>
}
复制代码
定义多个类,每一个类定义一套服务接口。而后分别实例化为多个对象,再使用准确的对象来调用接口。这种方法运行效率是最高的,可是在开发时,可能没法快速知道接口归属与哪一个服务,须要查看代码才能准确知晓,能够说是少了代码提示能力。git
interface ApiA {
@GET("user/loginWithScanCode")
fun methodA(@Query("id") id: Int): Observable<ResponseBody>
}
interface ApiB {
@GET("user/loginWithScanCode")
fun methodB(@Query("id") id: Int): Observable<ResponseBody>
}
复制代码
把全部接口都写在一个类中,而后根据服务地址分别实例化为多个对象。再准确调用方法,为了保证准确调用方法,能够给每一个接口加个服务名的前缀,以减小方法调错的问题。github
interface ApiAllInOne {
/************************** server A ****************************/
@GET("user/loginWithScanCode")
fun aMethod1(@Query("id") id: Int): Observable<ResponseBody>
/************************** server B ****************************/
@GET("user/loginWithScanCode")
fun bMethod1(@Query("id") id: Int): Observable<ResponseBody>
}
const val SERVER_HOST_A = "https://www.a.com/"
const val SERVER_HOST_B = "https://www.b.com/"
fun getApi(retrofit: Retrofit, host: String): ApiAllInOne {
return retrofit.newBuilder()
.baseUrl(host).build()
.create(ApiAllInOne::class.java)
}
fun showNomalUseCase(retrofit: Retrofit) {
val apiA = getApi(retrofit, SERVER_HOST_A)//save as single instance for repeated usage
apiA.aMethod1(1).subscribe()
apiA.bMethod1(1).subscribe()//invalid usage, but no compile error
val apiB = getApi(retrofit, SERVER_HOST_B)
apiB.bMethod1(1).subscribe()
apiB.aMethod1(1).subscribe()//invalid usage, but no compile error
}
复制代码
固然有了,并且超方便!api
(建议)在一个KT文件中定义全部接口,方便查找和维护。服务器
interface ApiHolder : ApiA, ApiB
@BaseUrl("https://www.a.com/")
interface ApiA {
@GET("user/loginWithScanCode")
fun methodA(@Query("id") id: Int): Observable<ResponseBody>
}
@BaseUrl("https://www.b.com/")
interface ApiB {
@GET("user/loginWithScanCode")
fun methodB(@Query("id") id: Int): Observable<ResponseBody>
}
复制代码
通常都须要个工具类的,方便配置拦截器等。若是没有自定义的需求,也能够直接实例化来用。markdown
能够重写invokeApi方法,全局给每一个Observable设定线程。网络
class ApiUtil : ApiHolderUtil<ApiHolder>(ApiHolder::class) {
companion object {
val apiUtil = ApiUtil()
val api = apiUtil.api
}
override fun invokeApi(api: Any, method: Method, args: Array<*>?): Any {
val observable = super.invokeApi(api, method, args) as Observable<*>
return observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
}
复制代码
还能够动态更新服务地址,好比实现测试服务和正式服务间切换。ide
//update api baseUrl when needed
apiUtil.updateApi(ApiA::class, "https://www.a2.com/")
复制代码
api.methodA(1).subscribe()
api.methodB(1).subscribe()
复制代码
dependencies {
implementation 'com.github.DonaldDu:ApiHolder:x.x.x'//JitPack version
}
复制代码
api 'com.squareup.okhttp3:okhttp:4.7.2'
api "com.squareup.retrofit2:retrofit:2.9.0"
api "com.squareup.retrofit2:converter-gson:2.9.0"
api "com.squareup.retrofit2:adapter-rxjava3:2.9.0"
api 'io.reactivex.rxjava3:rxandroid:3.0.0'
复制代码
能够根据须要调整为rxjava2,建议用最新的。
//重写ApiHolderUtil以下方法,RxJava3CallAdapterFactory ->RxJava2CallAdapterFactory便可。
protected open fun getRetrofit(client: OkHttpClient): Retrofit {
return Retrofit.Builder()
.validateEagerly(validateEagerly)
.addConverterFactory(getGsonConverterFactory())
.addCallAdapterFactory(RxJava3CallAdapterFactory.create())
.baseUrl("http://www.demo.com/")
.client(client)
.build()
}
复制代码
能够给每套服务设置不一样的超时
@BaseUrl("https://www.b.com/")
@Timeout(read = 100, timeUnit = TimeUnit.SECONDS)
interface ApiB {
@GET("user/loginWithScanCode")
fun methodB(@Query("id") id: Int): Observable<ResponseBody>
}
复制代码
开源不易,写文章更不易,劳烦你们给本文点个赞,能够的话,再给个star,感激涕零