Java 如何使用改型2处理动态JSON?

Java 如何使用改型2处理动态JSON?,java,android,json,retrofit,retrofit2,Java,Android,Json,Retrofit,Retrofit2,对于一个url,我有两种类型的响应。第一个是对象列表,第二个是错误响应对象 第一: [ { id: 1, title: "title1", description: "", position: 1000 }, { id: 2, title: "title3", description: "", position: 1000 }, {

对于一个url,我有两种类型的响应。第一个是对象列表,第二个是错误响应对象

第一:

[
    {
       id: 1,
       title: "title1",
       description: "",
       position: 1000
    },

    {
       id: 2,
       title: "title3",
       description: "",
       position: 1000
    },

    {
       id: 3,
       title: "title3",
       description: "",
       position: 1000
    }
 ]
第二:

{
   "status":"error",
   "error":"no token"
}
有没有办法用改装2来处理它?当服务器返回错误响应时,我有一个错误:“应为BEGIN\u数组,但在第1行第2列路径$处为BEGIN\u对象”

类别

public class Category{

    @SerializedName("id")
    @Expose
    private Id id;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("position")
    @Expose
    private Integer position;


    /**
     *
     * @return
     * The id
     */
    public Id getId() {
        return id;
    }

    /**
     *
     * @param id
     * The id
     */
    public void setId(Id id) {
        this.id = id;
    }

    /**
     *
     * @return
     * The title
     */
    public String getTitle() {
        return title;
    }

    /**
     *
     * @param title
     * The title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     *
     * @return
     * The description
     */
    public String getDescription() {
        return description;
    }

    /**
     *
     * @param description
     * The description
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     *
     * @return
     * The position
     */
    public Integer getPosition() {
        return position;
    }

    /**
     *
     * @param position
     * The position
     */
    public void setPosition(Integer position) {
        this.position = position;
    }



}
对服务器的请求:

 public void loadCategories(final boolean pullToRefresh) {

        Call<List<Category>> categoriesCall = mRequestHandler.getCategories(Keys.CATEGORY_CATALOGS);

        categoriesCall.enqueue(new Callback<List<Category>>() {
            @Override
            public void onResponse(Call<List<Category>> call, Response<List<Category>> response) {

                if (response.isSuccess()) {
                    getView().setData(response.body());
                }
            }

            @Override
            public void onFailure(Call<List<Category>> call, Throwable t) {

                getView().showError(null, pullToRefresh);
                Log.e("Error:", t.getMessage());

            }

        });


    }
public void loadCategories(最终布尔pullToRefresh){
调用categoriesCall=mRequestHandler.getCategories(key.CATEGORY\u目录);
categoriesCall.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issucess()){
getView().setData(response.body());
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
getView().showError(null,pullToRefresh);
Log.e(“错误:,t.getMessage());
}
});
}

我可以看到您的服务器在HTTP\U状态200上发送错误。 这就是为什么改装将其视为响应而不是错误。 对服务器端进行此更改将导致
public void onFailure(Call>Call,Throwable t)调用。

你能发布你的代码吗?使用一个包装器响应对象,你的数组将被放入其中,同时还有错误属性。任何一个都将为空,检查它以查看您得到的响应类型。我不知道是否可以在responseFix服务器的同时使用数组和对象。响应不能是数组或对象。否则,您将不得不在不进行改装的情况下手动解析对象并捕获该异常