欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Android项目多服务端接口适配(超简单)

程序员文章站 2022-06-25 11:55:24
现状android项目如果是多服务端接口时,一般怎么弄呢?方法1:服务器地址放在header中把服务器地址放在接口header中,然后通过拦截器来动态修改请求地址而实现的。除了默认服务器的接口,其它都...

现状

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<*>?): 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项目多服务端接口适配(超简单)的文章就介绍到这了,更多相关android多服务端接口适配 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!