2016-11-16 46 views
-1
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()爲空或不是。因爲如果圖像在數據庫中意味着我會得到「IMG」JSON對象,否則「IMG」對象不會作出迴應。如何檢查POJO類對象爲空或不在改造2.0

+0

嘗試從服務器使用IMAGEURL和使用畢加索將其設置成你的ImageView的。 – Lampard

+0

@Matteo nooo ..我不問關於設置Imageurl。我在問這個。如何檢查「getImg()」是否爲空。因爲我使用Gjson,而我的迴應是json對象並轉換爲POJO。如果在我的響應中不存在「img」對象,則表示Gjson顯示空指針異常。 –

+0

在這種情況下,只需使用try和catch塊來處理您的異常。 – Lampard

回答

0

您應該傳遞圖片URL並稍後加載(由Glide製作)。除非使用@Nullable註釋,否則默認情況下,所有整體對象和字符串都不爲空。

+0

{ 「HTTP_CODE」:200, 「狀態」: 「成功」, 「過期」:1487923755, 「用戶」:{ 「_id」: 「582c1346ae2aa1025a7caf5d」, 「電子郵件」:「例如@ gmail的。 COM 「 」名「: 」用戶名「, 」IMG「:{ 」小「: 」URL「, 」中等「: 」URL「, 」大「: 」URL「 」原「:」 url「 }, 」status「:0, 」socialToken「:」「 } }這是我的迴應。 「img」json objet有時會來,有時不會。它取決於後端dp。所以如果不會來我面臨空指針異常。 –

0

您可以檢查是否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(); 
    } 
}