詳解Android項目多服務端接口適配(超簡單)
現(xiàn)狀
Android項目如果是多服務端接口時,一般怎么弄呢?
方法1:服務器地址放在Header中
把服務器地址放在接口Header中,然后通過攔截器來動態(tài)修改請求地址而實現(xiàn)的。除了默認服務器的接口,其它都要加一個Header,有點麻煩。看起來也不爽,不簡潔。
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>}
方法2:多套服務類,實例化為多個對象,準確查找接口歸屬服務
定義多個類,每個類定義一套服務接口。然后分別實例化為多個對象,再使用準確的對象來調用接口。這種方法運行效率是最高的,但是在開發(fā)時,可能無法快速知道接口歸屬與哪個服務,需要查看代碼才能準確知曉,可以說是少了代碼提示能力。
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>}
方法3:全寫在一起,實例化為多個對象,準確調用方法
把所有接口都寫在一個類中,然后根據服務地址分別實例化為多個對象。再準確調用方法,為了保證準確調用方法,可以給每個接口加個服務名的前綴,以減少方法調錯的問題。
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}
有更簡單的方法嗎?
當然有了,而且超方便!
定義接口
(建議)在一個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> }
建工具類
一般都需要個工具類的,方便配置攔截器等。如果沒有自定義的需求,也可以直接實例化來用。
可以重寫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()) }}
動態(tài)更新服務地址
還可以動態(tài)更新服務地址,比如實現(xiàn)測試服務和正式服務間切換。
//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}
該項目使用的三方庫
OkHttp3 Retrofit2 rxjava3(可以修改為rxjava2)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’
其它說明
rxjava3 ->rxjava2
可以根據需要調整為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() }
Timeout
可以給每套服務設置不同的超時
@BaseUrl('https://www.b.com/')@Timeout(read = 100, timeUnit = TimeUnit.SECONDS)interface ApiB { @GET('user/loginWithScanCode') fun methodB(@Query('id') id: Int): Observable<ResponseBody>}
到此這篇關于詳解Android項目多服務端接口適配(超簡單)的文章就介紹到這了,更多相關Android多服務端接口適配 內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: