如何解决无法找到用于retrofit2.Response <okhttp3.responsebody>的呼叫适配器

时间:2018-07-09 15:29:21

标签: android retrofit2

我正在调查我的Android应用程序中的Retrofit2

我的gradle.build类似于:-

retrofit2Version = "2.4.0"
rxAndroidVersion = "2.0.2"
rxJavaVersion = "2.1.12"
okhttp3Version = "3.10.0"

implementation "com.squareup.okhttp3:logging-interceptor:$okhttp3Version"

implementation "com.squareup.retrofit2:retrofit:$retrofit2Version"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit2Version"
implementation "com.squareup.retrofit2:converter-scalars:$retrofit2Version"
implementation "com.squareup.retrofit2:converter-jackson:$retrofit2Version"

implementation "io.reactivex.rxjava2:rxandroid:$rxAndroidVersion"
implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"

我有三种使用同一@GET方法的版本

@Headers("accept: */*")
@GET("experiment")
Observable<Response<ResponseBody>> getObservableExperiment(@Header("Authorization") @NonNull final String authenticationToken, @Query("active") @NonNull final Boolean isActive);

@Headers("accept: */*")
@GET("experiment")
Call<ResponseBody> getExperiment(@Header("Authorization") @NonNull final String authenticationToken, @Query("active") @NonNull final Boolean isActive);

@Headers("accept: */*")
@GET("experiment")
Response<ResponseBody> getWrappedExperiment(@Header("Authorization") @NonNull final String authenticationToken, @Query("active") @NonNull final Boolean isActive);

getObservableExperiment()getExperiment()都可以正常工作

但是getWrappedExperiment()失败的原因是:-

 Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for retrofit2.Response<okhttp3.ResponseBody>.
      Tried:
       * retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
       * retrofit2.ExecutorCallAdapterFactory
        at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:241)
        at retrofit2.Retrofit.callAdapter(Retrofit.java:205)
        at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:238)

是否有retrofit2.Response<okhttp3.ResponseBody>的呼叫适配器? 我看了什么?

1 个答案:

答案 0 :(得分:0)

我看到您正在使用 RxJava。我的解决方案是将 Response 包裹在 Observable/Single 中:

@Headers("accept: */*")
@GET("experiment")
Single<Response<ResponseBody>> getWrappedExperiment(@Header("Authorization") @NonNull final String authenticationToken, @Query("active") @NonNull final Boolean isActive);

然后 flatMap 结果,以便您可以访问 Response 对象本身。