Retrofit2中无法访问okhttp3.ResponseBody的问题求助
Retrofit2开发依赖问题:无法访问okhttp3.ResponseBody
报错信息
Cannot access class 'okhttp3.ResponseBody'. Check your module classpath for missing or conflicting dependencies
问题代码
fun parseError(httpException: HttpException) { val errorBody = httpException.response()?.errorBody() }
当前依赖配置
implementation("com.squareup.okhttp3:okhttp:4.11.0") implementation("com.squareup.moshi:moshi-kotlin:1.15.0") implementation("com.squareup.retrofit2:retrofit:2.9.0") implementation("com.squareup.retrofit2:converter-moshi:2.9.0") implementation("com.squareup.okhttp3:logging-interceptor:4.11.0")
报错截图

解决方案
- 修复版本兼容性:Retrofit 2.9.0依赖的OkHttp版本为3.14.9,你当前使用的OkHttp 4.11.0版本跨度太大,导致类路径冲突。两种解决方式:
- 将Retrofit升级到2.11.0或更高版本(完全兼容OkHttp 4.x系列)
- 将OkHttp降级到3.14.9,与Retrofit 2.9.0的依赖版本匹配
- 补充适配依赖:确保引入Retrofit与OkHttp的关联依赖,避免类缺失:
implementation("com.squareup.retrofit2:adapter-okhttp3:2.9.0") - 清理重建项目:执行
./gradlew clean(或Android Studio中Build -> Clean Project),然后重新同步Gradle并构建,清除缓存中的冲突依赖
内容的提问来源于stack exchange,提问作者ant2009
相关产品推荐
相关产品推荐

