无法在retrofit2中访问responseBody。

huangapple go评论62阅读模式
英文:

Cannot access the responseBody in the retrofit2

问题

无法访问类'okhttp3.ResponseBody'。检查您的模块类路径以查找缺失或冲突的依赖项。

这是我的函数:

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")

这是图像,如您所见,errorBody() 下面出现了错误:

无法在retrofit2中访问responseBody。

英文:
Cannot access class 'okhttp3.ResponseBody'. Check your module classpath for missing or conflicting dependencies

This is my function:

fun parseError(httpException: HttpException) {
    val errorBody = httpException.response()?.errorBody()
}

These are the dependencies that I have included

    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")

This is the image as you can see there is a error below the errorBody()

无法在retrofit2中访问responseBody。

答案1

得分: 1

你缺少一个依赖项,其中包含OkHttp3.ResponseBody类。

implementation 'com.squareup.okhttp3:okhttp:{latest_version}'

希望这对你有所帮助。

英文:

You are missing one dependency which has OkHttp3.ResponseBody class

implementation 'com.squareup.okhttp3:okhttp:{latest_version}'

Hope this helps

答案2

得分: 1

编辑回答:

我发现在 Retrofit 的 2.9.0 版本中存在问题。更新到 2.8.2 版本后将会修复。

version.properties 文件

version.retrofit2=2.8.2


您可以通过使用 2.8.2 版本来解决此问题。

旧回答:

我认为您的项目是多模块的。您可以尝试使用 "api" 而不是 "implementation" 吗?

我发表此评论是因为我无法看到更多详细信息。

之所以找不到相关依赖项是因为我们项目的依赖项将第三方依赖项暴露到其 API 表面。

英文:

Edit Answer;

I found that there is a problem in version 2.9.0 of this retrofit. It will be fixed after updating to version 2.8.2.

version.properties file

version.retrofit2=2.8.2


You can use it by doing 2.8.2.

Old Answer;

I think your project is multi module. Can you try the "api" instead of "implementation"?

I'm making this comment because I can't see more details.

> The reason it cannot find related dependencies is that the dependency
> as reference of our project exposes third-party dependency onto its
> API surfaces.

答案3

得分: 1

看起来 Retrofit 依赖于 OkHttp 的版本 3.14.91,但你试图强制使用更新的版本,具体是 4.11.0。请注意,在版本 4.* 中,OkHttp 库中的所有类已迁移到 Kotlin,这可能是你遇到错误的原因。

英文:

It looks like Retrofit depends on version 3.14.9 of OkHttp, but you are trying to force a newer version, specifically 4.11.0. Please note that in version 4.*, all classes in the OkHttp library were migrated to Kotlin, and this migration might be the cause of the error you are encountering.

答案4

得分: 0

尝试移除 implementation("com.squareup.okhttp3:okhttp:4.11.0"). Retrofit2 已经将 OkHttp 客户端作为 api 依赖项。

英文:

Try remove implementation("com.squareup.okhttp3:okhttp:4.11.0"). Retrofit2 already have the OkHttp client depend as api.

huangapple
  • 本文由 发表于 2023年7月23日 15:23:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定