解决java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.Observable问题

使用Retrofit2结合RxJava2的网络请求方式,请求的时候报如下错误,原因是adapter-rxjava包引用错了,由于用的RxJava2,所以用的依赖的adapter库也应该是RxJava2的,报错的原因就是依赖城RxJava1的adapter了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
06-01 16:20:12.373 25257-25257/com.him.hisapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.him.hisapp, PID: 25257
java.lang.IllegalArgumentException: Unable to create call adapter for io.reactivex.Observable<com.him.hisapp.http.Resp.RespBase<java.util.List<com.him.hisapp.http.Resp.Account>>>
for method HttpService.login2
at retrofit2.Utils.methodError(Utils.java:52)
at retrofit2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:60)
at retrofit2.HttpServiceMethod.parseAnnotations(HttpServiceMethod.java:34)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:36)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at java.lang.reflect.Proxy.invoke(Proxy.java:397)
at $Proxy0.login2(Unknown Source)
at com.him.hisapp.http.HttpServiceImpl.login2(HttpServiceImpl.java:147)
at com.him.hisapp.MainActivity.login(MainActivity.java:31)
at com.him.hisapp.MainActivity.access$000(MainActivity.java:15)
at com.him.hisapp.MainActivity$1.onClick(MainActivity.java:25)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19888)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5276)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
Caused by: java.lang.IllegalArgumentException: Could not locate call adapter for io.reactivex.Observable<com.him.hisapp.http.Resp.RespBase<java.util.List<com.him.hisapp.http.Resp.Account>>>.
Tried:
* retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
* retrofit2.ExecutorCallAdapterFactory
at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:239)
at retrofit2.Retrofit.callAdapter(Retrofit.java:203)
at retrofit2.HttpServiceMethod.createCallAdapter(HttpServiceMethod.java:58)
... 20 more

Click and drag to move

错误的写法

1
2
3
4
5
6
7
8
9
10
11
dependencies {
implementation 'com.squareup.retrofit2:adapter-rxjava:2.5.0'
}

public Retrofit getRetrofit() {
retuan new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(getBaseUrl())
.build();
}

Click and drag to move

正确的写法

1
2
3
4
5
6
7
8
9
10
11
12
13
dependencies {
// 改成adapter-rxjava2
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
}

public Retrofit getRetrofit() {
retuan new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
// 改成RxJava2CallAdapterFactory
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(getBaseUrl())
.build();
}

Click and drag to move

总结

  • 依赖com.squareup.retrofit2:adapter-rxjava:2.5.0改成com.squareup.retrofit2:adapter-rxjava2:2.5.0
  • adapter RxJavaCallAdapterFactory改成RxJava2CallAdapterFactory