Java 如何处理改型2中错误的不同模式?

Java 如何处理改型2中错误的不同模式?,java,android,gson,retrofit2,Java,Android,Gson,Retrofit2,我有一个API,它返回以下模式之一: 成功(找到数据) 故障(未找到数据) 我想使用带有GSON的改型2来围绕这个API构建一个包装器并转换为POJO,但是我不确定如何处理API可能返回两个完全不同的模式这一事实。目前,我有以下课程: public class PaginatedResponse<T> { private int item_count; private int items_per_page; private int offset; pr

我有一个API,它返回以下模式之一:

成功(找到数据)

故障(未找到数据)

我想使用带有GSON的改型2来围绕这个API构建一个包装器并转换为POJO,但是我不确定如何处理API可能返回两个完全不同的模式这一事实。目前,我有以下课程:

public class PaginatedResponse<T> {
    private int item_count;
    private int items_per_page;
    private int offset;
    private List<T> items;

    public PaginatedResponse<T>(int item_count, int items_per_page, int offset, List<T> items) {
        this.item_count = item_count;
        this.items_per_page = items_per_page;
        this.offset = offset;
        this.items = items;
    }

    public List<T> getItems() {
        return this.items;
    }
}
对于我的API接口,我有:

public interface API {
    @GET("items")
    Call<PaginatedResponse<Item>> getItems();
}
似乎是因为失败的JSON缺少
items
属性,所以它将
List items
设置为
null

似乎是因为失败的JSON缺少items属性,所以它将List items设置为null

是的。您得到的是
NullPointerException
,因为您对null对象调用了
toString()
。这是预期的行为

解决方案

由于
错误
成功
的模式不同,因此需要创建一个同时包含这两个值的模型。下面给出一个简单的例子

ResponseModel.java


类响应模型{
//错误
个人成功;
私有最终错误;
//成功
私人最终整数项目计数;
//更多的成功价值观。。。
ResponseModel(布尔值成功、错误、整数项计数){
成功=成功;
this.error=错误;
this.item\u count=item\u count;
}
公共类错误{
私有最终整数码;
私有最终字符串消息;
私有错误(整数代码、字符串消息){
this.code=代码;
this.message=消息;
}
公共int getCode(){
返回码;
}
公共字符串getMessage(){
返回消息;
}
}
公共布尔getSuccess(){
回归成功;
}
公共错误getError(){
返回误差;
}
public int getItem_count(){
退货项目数量;
}
}
onResponse
方法中,您可以检查响应是否成功

ResponseModel ResponseModel=response.body();
if(responseModel.getError()==null){
//成功
doSomethingWithSuccess(responseModel.getItem\u count())
}否则{
//错误
doSomethingWithError(responseModel.getError())
}
public class PaginatedResponse<T> {
    private int item_count;
    private int items_per_page;
    private int offset;
    private List<T> items;

    public PaginatedResponse<T>(int item_count, int items_per_page, int offset, List<T> items) {
        this.item_count = item_count;
        this.items_per_page = items_per_page;
        this.offset = offset;
        this.items = items;
    }

    public List<T> getItems() {
        return this.items;
    }
}
public class Item {
    private int id;
    private String name;
    // ...
}
public interface API {
    @GET("items")
    Call<PaginatedResponse<Item>> getItems();
}
Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("http://localhost")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

API api = retrofit.create(API.class);

api.getItems().enqueue(new retrofit2.Callback<PaginatedResponse<Broadcast>>() {
    @Override
    public void onResponse(Call<PaginatedResponse<Broadcast>> call, Response<PaginatedResponse<Broadcast>> response) {
        Log.d("SUCCESS", response.body().getItems().toString());
    }

    @Override
    public void onFailure(Call<PaginatedResponse<Broadcast>> call, Throwable t) {
        Log.d("FAILURE", t.toString());
    }
}
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference