在集成Retrofit2+RxJava3时,会出现这个错误
java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.rxjava3.core.Observable<com.kotlin.base.data.protocol.BaseResp<java.lang.String>>
for method UserApi.register
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 $Proxy1.register(Unknown Source)
at com.kotlin.user.data.repository.UserRepository.register(UserRepository.kt:12)
at com.kotlin.user.service.impl.UserServiceImpl.register(UserServiceImpl.kt:15)
at com.kotlin.user.presenter.view.RegisterPresenter.onRegisterResult(RegisterPresenter.kt:14)
at com.kotlin.user.ui.activity.RegisterActivity$onCreate$1.onClick(RegisterActivity.kt:21)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.rxjava3.core.Observable<com.kotlin.base.data.protocol.BaseResp<java.lang.String>>.
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)
... 21 more
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
解决方法:
1.引用
implementation "com.squareup.retrofit2:adapter-rxjava3:2.9.0"
2.修改工程方法
retrofit = Retrofit.Builder().baseUrl(BaseConstant.SERVER_ADDRESS)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava3CallAdapterFactory.create()).build()