【问题标题】:Which rxjava3 retrofit-adapter should we use for Rxjava3我们应该为 Rxjava3 使用哪个 rxjava3 改造适配器
【发布时间】:2020-06-21 17:16:47
【问题描述】:

我正在使用 RxJava3 和改造,但我无法为 RxJava3 获得 rxjava3 改造适配器。

【问题讨论】:

标签: android retrofit rx-java rx-java2


【解决方案1】:

现在有一个 2.9.0 版本的官方 Retrofit 实现:

只需使用您创建 Retrofit 客户端的适配器:

val rxAdapter = RxJava3CallAdapterFactory.create()
retrofit = Retrofit.Builder().baseUrl(baseUrl)
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .client(httpClient)
            .addCallAdapterFactory(rxAdapter).build()

并在您的build.gradle 中包含 RxAdapter 依赖项:

implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'

https://github.com/square/retrofit/blob/master/CHANGELOG.md#version-290-2020-05-20

也来自文档:

与 RxJava 1 和 RxJava 2 适配器不同,RxJava 3 适配器的 create() 方法默认会产生异步 HTTP 请求。对于同步请求,使用 createSynchronous(),对于调度程序上的同步,使用 createWithScheduler(..)

【讨论】:

  • 作为唯一的版本让我感到不舒服...
【解决方案2】:

使用 @akarnokdRxJavaRetrofitAdapter 库。

implementation "com.github.akarnokd:rxjava3-retrofit-adapter:3.0.0"

【讨论】:

    【解决方案3】:

    使用

    implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 2012-01-16
      相关资源
      最近更新 更多