【问题标题】:How to access Gson objects that return from Api without return null pointer exception in android?如何在android中访问从Api返回的Gson对象而不返回空指针异常?
【发布时间】:2017-07-17 10:22:50
【问题描述】:

我使用 retofit 从 Api 获取数据,这是我的 ApiClient 类

public class ApiClient {
   private static final String BASE_URL = BuildConfig.BASE_URL;
   private static Retrofit retrofit = null;
   public static Retrofit getClient() {
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(120, TimeUnit.SECONDS)
            .connectTimeout(120, TimeUnit.SECONDS)
            .build();
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()))
                .build();
    }
    return retrofit;
  }
}

我使用GsonConverterFactory.create(new GsonBuilder().serializeNulls().create()) 序列化空键

这是我的 ApiInterface 方法

@FormUrlEncoded
@POST("data")
Call<MyCustomObject> getData(@Field("data") String request);

所以我将数据绑定到MyCustomObject,它可以如下

  public class MyCustomObject{

   @SerializedName("result")
   @Expose
   private Result result;
   @SerializedName("data")
   @Expose
   private Data Data;

   public Result getResult() {
       return result;
   }

   public void setResult(Result result) {
      this.result= result;
   }

   public Data getData() {
      return data;
   }

   public void setData(Data data) {
      this.data= data;
   }

 }

所以 Data 和 Result 对象可能有一些属性返回 null 并且它包含很多属性,当我在我的活动中使用它们时,它会通过空指针异常和应用程序崩溃

那么是否存在任何方法让我不必检查这些对象中的每个参数是否为空?

更新 1

关于更多信息,假设类数据如下

public class Data {

 @SerializedName("type")
 @Expose
 private String type;
 @SerializedName("properties")
 @Expose
 private String properties;

 public String getType() {
   return type;
 }

 public void setType(String type) {
   this.type = type;
 }

 public String getProperties() {
   return properties;
 }

 public void setProperties(String properties) {
   this.properties = properties;
 }
}

所以当我使用 getData().getType() 时,它可能会通过基于 api 的返回的空指针异常

【问题讨论】:

    标签: java android gson retrofit2


    【解决方案1】:

    您可以向模型类添加一些逻辑,因此如果 gson 为您提供一个空字段 (null),您可以在模型类中管理它,这样您就不需要在所有项目中管理 nullpointerexceptions。
    例如:

    public class MyCustomObject{
    
       @SerializedName("result")
       @Expose
       private Result result;
       @SerializedName("data")
       @Expose
       private Data Data;
    
       public Result getResult() {
           return (result != null) ? result : new Result();
       }
    
       public void setResult(Result result) {
          this.result= result;
       }
    
       public Data getData() {
          return (data != null) ? data : new Data();
       }
    
       public void setData(Data data) {
          this.data= data;
       }
    
     }
    

    注意获取模型的方法。
    对于对象,您需要找到一种方法来初始化其参数以表明它们未设置。例如,在您的数据模型中,如果您有一个名为 email 的字符串参数,您可以将该字符串设置为“未设置”。

    【讨论】:

      【解决方案2】:

      您可以在可能返回 null 的字段中添加 @Nullable 注解

      【讨论】:

      • 我更新了问题,所以如果我在尝试访问它时添加@Nullable,它不会通过空指针异常?
      • 我相信,试试看
      猜你喜欢
      • 2014-07-03
      • 2014-08-25
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多