I'm using Retrofit as a network client and sending Multiple APIs using RxJava's zip API, but when I create an instance for Retrofit, I got this error,
java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.rxjava3.core.Observable<java.lang.Object>
for method ListingApiService.sendMessageOffer1
at retrofit2.Utils.methodError(Utils.java:54)
at retrofit2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:116)
at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:67)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:39)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:202)
at retrofit2.Retrofit$1.invoke(Retrofit.java:160)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy2.sendMessageOffer1(Unknown Source)
at com.thunderboar.nutshellwhatsapp.BottomSheet.BottomSheetFragment$3.onClick(BottomSheetFragment.java:280)
at android.view.View.performClick(View.java:7441)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
at android.view.View.performClickInternal(View.java:7418)
at android.view.View.access$3700(View.java:835)
at android.view.View$PerformClick.run(View.java:28676)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.rxjava3.core.Observable<java.lang.Object>.
Tried:
* retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
* retrofit2.CompletableFutureCallAdapterFactory
* retrofit2.DefaultCallAdapterFactory
at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:272)
at retrofit2.Retrofit.callAdapter(Retrofit.java:237)
at retrofit2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:114)
... 20 more
this is the implementation
public interface ListingApiService {
@POST("{id}/msg")
Observable<Object> sendMessageOffer1(@Path("id") String pid,
@Body MyModel myModel
);
}
Retrofit instance
String BASE_URL="https://mywebsite.com";
String VERSION="v11.0/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL+VERSION)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(new OkHttpClient().newBuilder().addInterceptor(new
MyIntercepter()).build())
.build();
Intercepter class
public class MyIntercepter implements Interceptor {
private static final String TAG = "MyIntercepter";
@Override
public Response intercept(Chain chain) throws IOException {
Request request=chain.request();
Request newRequest=request.newBuilder()
.header("Authorization","Bearer token")
.header("Content-Type","application/json")
.build();
return chain.proceed(newRequest);
}
}
the dependencies
implementation "io.reactivex.rxjava3:rxjava:3.0.0"
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation("com.squareup.okhttp3:okhttp")
What went wrong with this implementation,
The issue because of the com.squareup.retrofit2:adapter-rxjava
, we have to update to version 3, like below
previous
com.squareup.retrofit2:adapter-rxjava2:2.9.0
need to update to
com.squareup.retrofit2:adapter-rxjava3:2.9.0