2

I am investigating Retrofit2 in my Android Application.

My gradle.build resembles:-

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"

I have three versions of the same @GET method

@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);

Both the getObservableExperiment() and getExperiment() work OK

however the getWrappedExperiment() fails with:-

 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)

Is there a call adapter for retrofit2.Response<okhttp3.ResponseBody>? What have I over looked?

Hector
  • 4,016
  • 21
  • 112
  • 211
  • Did you check this SO [question](https://stackoverflow.com/q/35233161/5180017) or this Retrofit related [issue](https://github.com/square/retrofit/issues/2028) raised in Github? – Shashanth Jul 09 '18 at 17:14
  • @Shashanth, I did see that question (and others just like it) and the GitHub issue, however they are specifically related to RxJava (if I am not mistaken?) and my issue is with a GET call that does not employ RxJava, so how would they assist me? – Hector Jul 10 '18 at 07:28
  • Try using `Call getWrappedExperiment(...)` instead of `Response getWrappedExperiment(...)`. Not sure why you are using `Response` instead of `Call`. – Shashanth Jul 10 '18 at 07:34
  • If possible post the code which shows, how the method `Response getWrappedExperiment(...)` is used in your project. – Shashanth Jul 10 '18 at 07:47

1 Answers1

0

I see you're using RxJava. The solution for me was to wrap the Response inside Observable/Single:

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

Then flatMap the result so you have access to the Response object itself.

Simon
  • 2,643
  • 3
  • 40
  • 61