使用Retrofit與RxJava須要注意的地方

1、報錯狀況

* What went wrong:
Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> More than one file was found with OS independent path 'META-INF/rxjava.properties'複製代碼

前幾天在首次使用Retrofit和RxJava報錯,當時一直沒有找到解決方案,而且網上百度的方案也沒法解決。java

後來無心中看到大佬寫的一篇文章才得以解決該錯誤。對應文章(文中4.3步驟實現有介紹):react


2、解決方案

參看這篇文章後,再回看本身的build.gradle,才知道對應的錯誤在哪兒android

原來是這樣寫的:bash

//支持Gson解析
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

    //添加RxJava
    implementation 'io.reactivex.rxjava2:rxjava:2.2.14'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'複製代碼

錯誤就在這個庫「implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'」,該庫時對應於retrofit2RxJava,而不是RxJava2網絡

僅僅是這個錯誤才致使上述報錯的緣由,app

附上正確的庫:工具

//retrofit開源的網絡請求工具
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'

    //添加RxJava
    implementation 'io.reactivex.rxjava2:rxjava:2.2.14'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

    //銜接Retrofit 和 RxJava
    //此處必定要注意使用RxJava2的版本
    implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

    //支持Gson解析
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.google.code.gson:gson:2.8.5'
複製代碼
相關文章
相關標籤/搜索