1

Need help with handling different JSON field types response in Retrofit 2

The serverapi return 2 different types of response and both in Http 200 OK:

if success, server will return this response: {"error_code":"0000","error_message":"success!",item_id:"SHVR25","description":{"DENOMINATION":"25","PRICE":"28000"}}

not success,: {"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 Description

    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;
    }
}

CallBack

    @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());
                    }
                }
    );

Actually my problem similar to this cases: How to handle response which can be different Type in Retrofit 2

Problem: call back always called onFailed and said that java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 236 path $.description

Question: How i can handle different types of response retrofit using POJO as callback not JsonElement, and without changing the server response?

I have try this 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 (it using JsonElement as callback response)

Community
  • 1
  • 1

2 Answers2

0

Use Object type ,

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

In your Bill class

Onkar
  • 679
  • 1
  • 8
  • 25
0

Whenever you hit the api, there are two possibilities. Success or failure. To get the error body we use response.errorBody and to parse the error body the kotlin code is written below:

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

The data class is:

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

The error class contains the fields that your api error contains.

To get the error messsage, we can call the method like this:

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