提问者:小点点

如何解决找不到Retrefit的调用适配器2。响应<okhttp3。响应体>


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

我的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);

getWatable实验()get实验()都可以正常工作

然而,getWrappeAthment()失败:-

 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)

是否有改造2的调用适配器。响应


共1个答案

匿名用户

我看到你在使用RxJava。对我来说,解决方案是将Response包装在可观察/单个中:

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

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