温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android项目多服务端接口适配的方法有哪些

发布时间:2020-08-12 15:22:25 来源:亿速云 阅读:195 作者:小新 栏目:开发技术

小编给大家分享一下Android项目多服务端接口适配的方法有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

现状

Android项目如果是多服务端接口时,一般怎么弄呢?

方法1:服务器地址放在Header中

把服务器地址放在接口Header中,然后通过拦截器来动态修改请求地址而实现的。除了默认服务器的接口,其它都要加一个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:多套服务类,实例化为多个对象,准确查找接口归属服务

定义多个类,每个类定义一套服务接口。然后分别实例化为多个对象,再使用准确的对象来调用接口。这种方法运行效率是最高的,但是在开发时,可能无法快速知道接口归属与哪个服务,需要查看代码才能准确知晓,可以说是少了代码提示能力。

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<*>&#63;): Any {
    val observable = super.invokeApi(api, method, args) as Observable<*>
    return observable.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
  }
}

动态更新服务地址

还可以动态更新服务地址,比如实现测试服务和正式服务间切换。

//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项目多服务端接口适配的方法有哪些的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI