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

时间:2017-12-13 04:56:51

标签: android json retrofit2 jsonschema2pojo gson

改造2

处理不同的JSON字段类型响应需要帮助

serverapi返回2种不同类型的响应,并且都在Http 200中确定:

如果成功,服务器将返回此响应:  {"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 Bill

    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?(但它适用于改造1)和How to handle response which can be different Type in Retrofit 2(它使用JsonElement作为回调响应)

2 个答案:

答案 0 :(得分:0)

使用对象类型

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

在比尔课程中

答案 1 :(得分:0)

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

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

数据类为:

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

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

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

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