Android OkHttp Retrofit RxJava集成的常见Bug

在使用OkHttp Retrofit RxAndroid的过程中遇到了一些莫名的Bug。如下:

  1. ArrayIndexOutOfBoundsException

    Throwing new exception ‘length=6; index=7’ with unexpected pending exception:java.lang.ArrayIndexOutOfBoundsException: length=6; index=6

    解决办法:这个网上不少都是说取消勾选Instant Run,就每次运行都重新编译。但是这个功能在Android Studio3.5版本后废弃了。

    我试了下这样并不能解决问题,然后发现是因为Retrofit、RxAndroid和adapter-rxjava的版本不匹配引起的。RxAndroid是3.0,原来的adapter-rxjava使用的版本是2。

    更改后的依赖是这样的

    	// okHttp 3.14
        implementation 'com.squareup.okhttp3:okhttp:3.14.9'
        // Retrofit 2.9
        implementation 'com.squareup.retrofit2:retrofit:2.9.0'
        // Convert Google Json(Convert the result to Model)
        implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
        // Retrofit CallAdapter convert to RxJava
        implementation 'com.squareup.retrofit2:adapter-rxjava3:2.9.0'
        // RxAndroid 3.0
        implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    
  2. BootstrapMethodError

    java.lang.BootstrapMethodError: Exception from call site #1 bootstrap method…

    解决办法:这个是因为Retrofit中使用了JDK8中的新特性,Lamdba等,所以我们要在app的build.gradle中添加支持。

    	// Support new features of JDK1.8
        compileOptions {
          
          
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    

    这样就可以解决了。
    上面这两个问题不太容易定位,贴出来希望可以有帮助,总之,还是要细心。

猜你喜欢

转载自blog.csdn.net/A_Intelligence/article/details/109517279