Common bugs of Android OkHttp Retrofit RxJava integration

I encountered some inexplicable bugs while using OkHttp Retrofit RxAndroid. as follows:

  1. ArrayIndexOutOfBoundsException

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

    Solution: Many people on the Internet say that you uncheck Instant Run and recompile each time you run it. But this feature was abandoned after Android Studio 3.5.

    I tried this but it didn't solve the problem, and then I found that it was caused by the mismatch between the versions of Retrofit, RxAndroid and adapter-rxjava. RxAndroid is 3.0, and the version used by the original adapter-rxjava is 2.

    The changed dependency is like this

    	// 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…

    Solution: This is because Retrofit uses new features in JDK8, Lamdba, etc., so we need to add support in the app's build.gradle.

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

    This can be solved.
    The above two issues are not easy to locate, post them and hope that they can be helpful. In short, we still need to be careful.

Guess you like

Origin blog.csdn.net/A_Intelligence/article/details/109517279