【问题标题】:handle response which has different type in Retrofit 2在改造 2 中处理具有不同类型的响应
【发布时间】:2018-05-26 21:54:37
【问题描述】:

Retrofit 2

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

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

如果成功,服务器会返回这个响应: {"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?(但它适用于改造 1)和 How to handle response which can be different Type in Retrofit 2(它使用 JsonElement 作为回调响应)

【问题讨论】:

  • 如果 onFailed 被调用,那么服务器响应有问题
  • 您的预期响应是 Json 对象,但它正在返回 String?
  • 请验证您的服务器响应。看起来它以“开头。改造预计以 {.
  • @AbhayBohra 第一个响应和第二个响应之间的区别在于描述字段,一个返回 json 对象另一个返回字符串先生。
  • @ShylendraMadda 我期待两位先生,但我认为问题出在 gson 转换器上。当 gson 将我的服务器响应转换为 pojo 时发生错误,第一个响应很好,但第二个响应不是。因为第二个响应返回非字符串先生。

标签: android json retrofit2 jsonschema2pojo gson


【解决方案1】:

使用Object类型,

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

在你的 Bill 类中

【讨论】:

    【解决方案2】:

    每当您点击 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
    

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-10
      • 2018-12-16
      相关资源
      最近更新 更多