1

I was using retrofit 2 for a project and i cannot parse the response of json.

Retrofit retrofit = new  Retrofit.Builder().baseUrl("BASE_URL").addConverterFactory(GsonConverterFactory    .create()).build();
final RetrofitApi request = retrofit.create(RetrofitApi.class);
LoginModel loginModel = new LoginModel("user@mail.com",bijoy@123#");
Call<List<LoginResponse>> listCall = request.Loginner(loginModel);
listCall.enqueue(new Callback<List<LoginResponse>>() {
       @Override
       public void onResponse(Call<List<LoginResponse>> call, Response<List<LoginResponse>> response) {
             List<LoginResponse> loginModelList = new ArrayList<>();
             loginModelList = response.body();  

The json response was like

 {
      "response": {
        "errorCode": "",
        "errorMsg": "",
        "successCode": "SUB001",
        "successMsg": "Login successfully",
        "data": {
          "user_details": {
            "salutation": "Mr.",
            "first_name": "User",
            "last_name": "R",
            "email": "user@mail.com",
            "alternative_email": "",
            "mobile_number": "54312",
            "phone_number": "",
            "title_position": "",
            "department": "",
            "city": "",
            "street": "",
            "state": "",
            "postcode": "",
            "display_name": "Bijoy R",
            "assistant_details": "",
            "country_name": "India",
            "institution_name": "KLO",
            "user_type": "commercial"
          },
          "payment_details": [
            {
              "dateandtime": "28-07-2016 17:4736",
              "payment_mode": "DD",
              "items": "Accompanying Person",
              "transactionId": "",
              "amount": "3000.00",
              "status": "Failed"   }        ],
        }
      }
    } 

How to write getters and setters for this type of json.

Ishita Sinha
  • 2,168
  • 4
  • 25
  • 38
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77

2 Answers2

4

Try to replace Call<List<LoginResponse>> with Call<LoginResponse> and Response<List<LoginResponse>> to Response<LoginResponse>

Nik Kober
  • 868
  • 11
  • 16
0

change the Retrofit API Interface return type is Call<ServerResponse<LoginResponse>>, fllow this code:

public class ServerResponse<T> {

    private ResponseStatus response;
    private T data;

    public ResponseStatus getResponse() {
        return response;
    }

    public void setResponse(ResponseStatus response) {
        this.response = response;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

public class ResponseStatus {

    private String errorCode;
    private String errorMsg;
    private String successCode;
    private String successMsg;

    public String getErrorCode() {
        return errorCode;
    }

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

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getSuccessCode() {
        return successCode;
    }

    public void setSuccessCode(String successCode) {
        this.successCode = successCode;
    }

    public String getSuccessMsg() {
        return successMsg;
    }

    public void setSuccessMsg(String successMsg) {
        this.successMsg = successMsg;
    }
}

public class LoginResponse {

    @SerializedName("userDetails")
    private User userDetails;

    @SerializedName("payment_details")
    private List<PaymentDetail> paymentDetails;

    public User getUserDetails() {
        return userDetails;
    }

    public void setUserDetails(User userDetails) {
        this.userDetails = userDetails;
    }

    public List<PaymentDetail> getPaymentDetails() {
        return paymentDetails;
    }

    public void setPaymentDetails(List<PaymentDetail> paymentDetails) {
        this.paymentDetails = paymentDetails;
    }
}

public class User {
    /**
     * Ignore implement
     */
}

public class PaymentDetail {
    /**
     * Ignore implement
     */
}

use above example to write your model class related to the JSON response format. I recommend your JSON response use CamelCase style ,combine better with GSON. I'm sorry, My English is so limited.

Colin Chen
  • 61
  • 2