【问题标题】:Retrofit2: How to properly check the validity of response.body()?Retrofit2:如何正确检查 response.body() 的有效性?
【发布时间】:2017-08-01 09:58:03
【问题描述】:

自版本 retrofit2.retrofit:2.3.0 以来,即使在之前检查 null 的正文时,我也会在 response.body() 上收到 NullPointerException 警告:

Method invocation 'getCar()' may produce 'java.lang.NullPointerException'

2.3.0 的更新日志中有一个与空检查相关的条目:

Retrofit 现在使用 @Nullable 来注释所有可能为空的值。 [...] 我们使用 @ParametersAreNonnullByDefault 并且所有参数和返回类型永远不会为空,除非显式注释 @Nullable。

这是预期的行为吗?在我看来response.body() 应该是不可变的,所以我在图片1 中的检查不应该显示警告。

这不是关于NullPointerExceptions 的问题 - 这是关于如何正确处理 Retrofit2 响应的方式。为了暂时没有警告,我必须这样做:

if(response != null) {
    CarResponseBody body = response.body();
    if (body != null && body.getCar() != null){
        CarResponse car = body.getCar();
    }
}

很多代码只是为了检查是否有有效的响应......

【问题讨论】:

  • 可能是警告,因为body() 可能会在第一次调用时返回非空值,然后在第二次调用时返回空值?
  • 警告理论上是正确的。仅仅因为 body() 在您第一次调用它时返回了一些东西,并不意味着它不会在您下次调用它时返回 null。
  • 我知道 NullPointerException 是什么,谢谢!这个问题更像是 Jon Skeet 说的:为什么 body() 在第二次调用时可能会返回不同的东西?我会稍微编辑一下问题。
  • 如果有来自 Retrofit 的人回答这个问题或者至少提供一些建议会很好,因为他们建议在 stackoverflow 中发布这些内容,而不是在 GitHub 上打开问题

标签: java android retrofit retrofit2


【解决方案1】:

这是预期的行为吗?

如果您查看Response<T> 的JavaDoc,您可以阅读

@Nullable public T body() 成功响应的反序列化响应正文。
Javadoc: Response

正如它所暗示的,如果响应成功,body() 将不会是 null。要检查是否成功,你有 isSuccessful() which

如果 code() 在 [200..300) 范围内,则返回 true。

所以@Nullable 是一个有效的选择,因为在任何不成功的情况下响应都可以是null,例如没有网络、无效请求或其他错误。

IDE 中的提示是一个 lint 警告,用于检查源代码中常见错误或错误的可能来源。

这就是 为什么 body() 可能是 null 以及为什么 lint 首先将其报告为警告。

在我看来 response.body() 应该是不可变的,所以我在图 1 中的检查不应该显示警告。

理论上你是对的。你和我都知道body() 是不可变的,但这里的问题是 this lint check 无法知道。

T res1 = response.body(); // could be null
T res2 = response.body(); // ...maybe still null?

Lint 是一种静态源代码和字节码分析器,有助于防止常见的错误和错误,其中一项 lint 检查试图防止 NPE。如果你注释了一个方法@Nullable,所有的检查都知道返回值可能null,如果你尝试直接操作调用的结果,它会发出警告。 p>

// does response return the same value twice? who knows?
response.body() != null && response.body().getCar() != null

您处理响应的方式实际上是消除 lint 警告的唯一方法,而不是抑制或禁用它。

通过将其分配给局部变量,您可以确保某个值在某些时候不是null,并且将来不会变为null,并且lint 也能够看到这一点。

CarResponseBody body = response.body(); // assign body() to a local variable
if (body != null && body.getCar() != null) { // variable is not null
    CarResponse car = body.getCar(); // safe to access body, car is also not null
    // everything is fine!
}

这是预期的行为吗?

是的。 @Nullable 是暗示方法可能返回 null 的好方法,如果您在某些路径上返回 null,您也应该在自己的代码中使用它因为 lint 可以警告可能的 NullPointerExceptions .

如果某个方法可能返回 null,您必须将其分配给本地字段并检查该字段中的 null 值,否则您可能会面临值可能已更改的风险。

Car car = response.getBody(); // car could be null
if(car != null) {
  // car will never be null
}

不同的选择/方法

我看到您似乎还另外将您的响应对象包装在一个附加层中。

CarResponseBody body = response.body();
Car car = body.getCar()

如果您想从代码中消除复杂性,您应该在早期阶段了解如何删除此包装 *ResponseBody。您可以通过注册自己的 Converter 并在那里添加额外的处理来实现。您可以在 Retrofit talk by Jake Wharton 上查看更多相关信息

另一种完全不同的方法是将 RxJava 与 Retrofit 结合使用,这样就无需自己检查响应。您将获得成功或错误,您可以通过 Rx 方式处理。

【讨论】:

  • 感谢您的精彩解释!我会看看转换器,这似乎是要走的路。
【解决方案2】:

我通常在 WhatResponse 类中创建一个 isValid() 或 hasResults() 方法

boolean isValid(){
return getCar() != null && getWheel() != null && somethingElse
}

用法

if(response.isValid()){
//do your other if's
}

不需要检查空响应。 errorBody() 将不为 null,否则将在此之前调用 retrofit2.Callback onFailure() 方法

【讨论】:

    【解决方案3】:

    试试这个

     if (response != null) {CarResponseBody body= new CarResponseBody(); if (response.raw().code() == 200 && response.body()!=null) {
    body = response.body();if(body.getCar()!=null){ CarResponse car =body.getCar()}}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-15
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      相关资源
      最近更新 更多