【问题标题】:How to Check POJO class object is null or not in retrofit 2.0如何在改造 2.0 中检查 POJO 类对象是否为空
【发布时间】:2017-03-30 08:03:01
【问题描述】:
public class User {

    @SerializedName("_id")
    @Expose
    private String id;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("img")
    @Expose
    private Img img;
    @SerializedName("socialToken")
    @Expose
    private String socialToken;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Img getImg() {
        return img;
    }

    public void setImg(Img img) {
        this.img = img;
    }

}

在我的 pojo 课程中,如何检查 getImg() 是否为空。因为如果图像在 db 中意味着我将获得“img”json 对象,否则“img”对象将不会响应。

【问题讨论】:

  • 尝试使用来自服务器的 imageUrl 并使用 Picasso 将其设置在您的 Imageview 中。
  • @Matteo nooo .. 我不是在询问设置 Imageurl 。我在问那个。如何检查“getImg()”是否为空。因为我正在使用 Gjson 并且我的响应是 json 对象并转换为 POJO 。如果我的响应中没有“img”对象意味着 Gjson 显示空指针异常。
  • 在这种情况下,只需使用 try 和 catch 块来处理您的异常。
  • @Matteo 。是的,我可以,但如果我使用 try and catch .remaining 代码将不会执行。它去抓块。那么有没有其他方法可以处理。但在默认 JSON 中。有一个名为“isNull”的选项来检查是否为空。像这样,Gjson里有什么吗?
  • 我已经添加了一个答案,请检查它是否可以处理你的空指针。

标签: android gson retrofit retrofit2


【解决方案1】:

您应该传递图像 URL 并稍后加载(通过 Glide)。所有整型对象和字符串默认不为空,除非你使用@Nullable注解。

【讨论】:

  • { "http_code": 200, "status": "success", "expires": 1487923755, "user": { "_id": "582c1346ae2aa1025a7caf5d", "email": "example@gmail .com", "name": "username", "img": { "small": "url", "medium": "url", "large": "url" "original": "url" }, " status": 0, "socialToken": "" } } 这是我的回应。 “img” json 对象有时来,有时不来。这取决于后端 dp 。所以如果不来,我将面临空指针异常。
【解决方案2】:

您可以通过以下代码检查 JsonObject 是否为空:

public final class JsonObject extends JsonElement {
  private final LinkedTreeMap<String, JsonElement> members =
      new LinkedTreeMap<String, JsonElement>();

  @Override
  JsonObject deepCopy() {
    JsonObject result = new JsonObject();
    for (Map.Entry<String, JsonElement> entry : members.entrySet()) {
      result.add(entry.getKey(), entry.getValue().deepCopy());
    }
    return result;
  }

  /**
   * Adds a member, which is a name-value pair, to self. The name must be a String, but the value
   * can be an arbitrary JsonElement, thereby allowing you to build a full tree of JsonElements
   * rooted at this node.
   *
   * @param property name of the member.
   * @param value the member object.
   */
  public void add(String property, JsonElement value) {
    if (value == null) {
      value = JsonNull.INSTANCE;
    }
    members.put(property, value);
  }

  /**
   * Removes the {@code property} from this {@link JsonObject}.
   *
   * @param property name of the member that should be removed.
   * @return the {@link JsonElement} object that is being removed.
   * @since 1.3
   */
  public JsonElement remove(String property) {
    return members.remove(property);
  }

  /**
   * Convenience method to add a primitive member. The specified value is converted to a
   * JsonPrimitive of String.
   *
   * @param property name of the member.
   * @param value the string value associated with the member.
   */
  public void addProperty(String property, String value) {
    add(property, createJsonElement(value));
  }

  /**
   * Convenience method to add a primitive member. The specified value is converted to a
   * JsonPrimitive of Number.
   *
   * @param property name of the member.
   * @param value the number value associated with the member.
   */
  public void addProperty(String property, Number value) {
    add(property, createJsonElement(value));
  }

  /**
   * Convenience method to add a boolean member. The specified value is converted to a
   * JsonPrimitive of Boolean.
   *
   * @param property name of the member.
   * @param value the number value associated with the member.
   */
  public void addProperty(String property, Boolean value) {
    add(property, createJsonElement(value));
  }

  /**
   * Convenience method to add a char member. The specified value is converted to a
   * JsonPrimitive of Character.
   *
   * @param property name of the member.
   * @param value the number value associated with the member.
   */
  public void addProperty(String property, Character value) {
    add(property, createJsonElement(value));
  }

  /**
   * Creates the proper {@link JsonElement} object from the given {@code value} object.
   *
   * @param value the object to generate the {@link JsonElement} for
   * @return a {@link JsonPrimitive} if the {@code value} is not null, otherwise a {@link JsonNull}
   */
  private JsonElement createJsonElement(Object value) {
    return value == null ? JsonNull.INSTANCE : new JsonPrimitive(value);
  }

  /**
   * Returns a set of members of this object. The set is ordered, and the order is in which the
   * elements were added.
   *
   * @return a set of members of this object.
   */
  public Set<Map.Entry<String, JsonElement>> entrySet() {
    return members.entrySet();
  }

  /**
   * Returns the number of key/value pairs in the object.
   *
   * @return the number of key/value pairs in the object.
   */
  public int size() {
    return members.size();
  }

  /**
   * Convenience method to check if a member with the specified name is present in this object.
   *
   * @param memberName name of the member that is being checked for presence.
   * @return true if there is a member with the specified name, false otherwise.
   */
  public boolean has(String memberName) {
    return members.containsKey(memberName);
  }

  /**
   * Returns the member with the specified name.
   *
   * @param memberName name of the member that is being requested.
   * @return the member matching the name. Null if no such member exists.
   */
  public JsonElement get(String memberName) {
    return members.get(memberName);
  }

  /**
   * Convenience method to get the specified member as a JsonPrimitive element.
   *
   * @param memberName name of the member being requested.
   * @return the JsonPrimitive corresponding to the specified member.
   */
  public JsonPrimitive getAsJsonPrimitive(String memberName) {
    return (JsonPrimitive) members.get(memberName);
  }

  /**
   * Convenience method to get the specified member as a JsonArray.
   *
   * @param memberName name of the member being requested.
   * @return the JsonArray corresponding to the specified member.
   */
  public JsonArray getAsJsonArray(String memberName) {
    return (JsonArray) members.get(memberName);
  }

  /**
   * Convenience method to get the specified member as a JsonObject.
   *
   * @param memberName name of the member being requested.
   * @return the JsonObject corresponding to the specified member.
   */
  public JsonObject getAsJsonObject(String memberName) {
    return (JsonObject) members.get(memberName);
  }

  @Override
  public boolean equals(Object o) {
    return (o == this) || (o instanceof JsonObject
        && ((JsonObject) o).members.equals(members));
  }

  @Override
  public int hashCode() {
    return members.hashCode();
  }
}

【讨论】:

    猜你喜欢
    • 2013-10-11
    • 1970-01-01
    • 2012-03-13
    • 2019-06-09
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多