处理 Retrofit 2 中不同类型的响应

handle response which has different type in Retrofit 2

需要帮助 处理 Retrofit 2

中不同的 JSON 字段类型 响应

serverapi return 2 种不同类型的响应并且都在 Http 200 中:

如果成功,服务器将return响应: {"error_code":"0000","error_message":"success!",item_id:"SHVR25","description":{"DENOMINATION":"25","PRICE":"28000"}}

不成功,: {"error_code":"1111","error_message":"failed!",item_id:"SHVR10","description":""}

POJO 法案

    public class Bill{

    @SerializedName("error_code")
    @Expose
    public String errorCode;

    @SerializedName("error_message")
    @Expose
    public String errorMessage;

    @SerializedName("item_id")
    @Expose
    public String itemId;

    @SerializedName("description")
    @Expose
    public Description descriptionss;

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public Description getDescriptionss() {
        return descriptionss;
    }

    public void setDescriptionss(Description descriptionss) {
        this.descriptionss = descriptionss;
    }
    public Description getDescriptionss() {
        return descriptionss;
    }
    }

POJO 描述

    public class Description{
    @SerializedName("DENOMINATION")
    @Expose
    public String dENOMINATION;
    @SerializedName("PRICE")
    @Expose
    public String pRICE;


    public String getdENOMINATION() {
        return dENOMINATION;
    }

    public void setdENOMINATION(String dENOMINATION) {
        this.dENOMINATION = dENOMINATION;
    }

    public String getpRICE() {
        return pRICE;
    }

    public void setpRICE(String pRICE) {
        this.pRICE = pRICE;
    }
}

回调

    @Override
    public void onResponse(Call<Bill> call, Response<Bill> response) {
        if(response.isSuccessful()){
            Bill bill= response.body();
            Timber.d("bill contains=="+bill.toString());
                            }
        }else{
            Timber.d("response is not success!");}
    }
    @Override
    public void onFailure(Call<JsonElement> call, Throwable t) {
            Timber.e("retrofit failed.... throwable:"+t.toString());
                    }
                }
    );

其实我的问题和这个案例类似:How to handle response which can be different Type in Retrofit 2

问题:回调总是调用onFailed并表示java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 236 path $.description

问题: 我如何使用 POJO 作为回调而不是 JsonElement 来处理不同类型的响应改造,而不更改服务器响应?

我试过了 How to handle Dynamic JSON in Retrofit? ( but it works for retrofit 1 ) and How to handle response which can be different Type in Retrofit 2(它使用 JsonElement 作为回调响应)

使用对象类型,

 @SerializedName("description")
  @Expose
  public Object descriptionss;

在您的账单中class

每当您点击 api 时,有两种可能性。成功或失败。 为了获取错误主体,我们使用 response.errorBody 并解析错误主体,kotlin 代码如下:

val loginRequestError = { response: Response<YourResponse> ->
        SocietyRetrofit.retrofit.responseBodyConverter<Error>(Error::class.java, arrayOf<Annotation>()).convert(response.errorBody())
    }

数据class是:

data class Error(var error: String, var message: String)

错误 class 包含您的 api 错误包含的字段。

要获取错误消息,我们可以这样调用方法:

val errorMessage = loginRequestError(<Your api response>).message