Manoj Frekzz Manoj Frekzz - 3 months ago 11
Android Question

Expected BEGIN_ARRAY but was BEGIN_OBJECT in retrofit 2?

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.

This is the LoginResponse.class

public class LoginResponse {


@SerializedName("response")
public Response response;

public Response getResponse() {
return response;
}

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

public static class UserDetails {
@SerializedName("salutation")
public String salutation;
@SerializedName("first_name")
public String firstName;
@SerializedName("last_name")
public String lastName;
@SerializedName("email")
public String email;
@SerializedName("alternative_email")
public String alternativeEmail;
@SerializedName("mobile_number")
public String mobileNumber;
@SerializedName("phone_number")
public String phoneNumber;
@SerializedName("title_position")
public String titlePosition;
@SerializedName("department")
public String department;
@SerializedName("city")
public String city;
@SerializedName("street")
public String street;
@SerializedName("state")
public String state;
@SerializedName("postcode")
public String postcode;
@SerializedName("display_name")
public String displayName;
@SerializedName("assistant_details")
public String assistantDetails;
@SerializedName("country_name")
public String countryName;
@SerializedName("institution_name")
public String institutionName;
@SerializedName("user_type")
public String userType;

public String getSalutation() {
return salutation;
}

public void setSalutation(String salutation) {
this.salutation = salutation;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getAlternativeEmail() {
return alternativeEmail;
}

public void setAlternativeEmail(String alternativeEmail) {
this.alternativeEmail = alternativeEmail;
}

public String getMobileNumber() {
return mobileNumber;
}

public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getTitlePosition() {
return titlePosition;
}

public void setTitlePosition(String titlePosition) {
this.titlePosition = titlePosition;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getPostcode() {
return postcode;
}

public void setPostcode(String postcode) {
this.postcode = postcode;
}

public String getDisplayName() {
return displayName;
}

public void setDisplayName(String displayName) {
this.displayName = displayName;
}

public String getAssistantDetails() {
return assistantDetails;
}

public void setAssistantDetails(String assistantDetails) {
this.assistantDetails = assistantDetails;
}

public String getCountryName() {
return countryName;
}

public void setCountryName(String countryName) {
this.countryName = countryName;
}

public String getInstitutionName() {
return institutionName;
}

public void setInstitutionName(String institutionName) {
this.institutionName = institutionName;
}

public String getUserType() {
return userType;
}

public void setUserType(String userType) {
this.userType = userType;
}
}

public static class PaymentDetails {
@SerializedName("dateandtime")
public String dateandtime;
@SerializedName("payment_mode")
public String paymentMode;
@SerializedName("items")
public String items;
@SerializedName("transactionId")
public String transactionid;
@SerializedName("amount")
public String amount;
@SerializedName("status")
public String status;

public String getDateandtime() {
return dateandtime;
}

public void setDateandtime(String dateandtime) {
this.dateandtime = dateandtime;
}

public String getPaymentMode() {
return paymentMode;
}

public void setPaymentMode(String paymentMode) {
this.paymentMode = paymentMode;
}

public String getItems() {
return items;
}

public void setItems(String items) {
this.items = items;
}

public String getTransactionid() {
return transactionid;
}

public void setTransactionid(String transactionid) {
this.transactionid = transactionid;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}
}

public static class Data {
@SerializedName("user_details")
public UserDetails userDetails;
@SerializedName("payment_details")
public List<PaymentDetails> paymentDetails;
@SerializedName("latest_receipt_url")
public String latestReceiptUrl;

public UserDetails getUserDetails() {
return userDetails;
}

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

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

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

public String getLatestReceiptUrl() {
return latestReceiptUrl;
}

public void setLatestReceiptUrl(String latestReceiptUrl) {
this.latestReceiptUrl = latestReceiptUrl;
}
}

public static class Response {
@SerializedName("errorCode")
public String errorcode;
@SerializedName("errorMsg")
public String errormsg;
@SerializedName("successCode")
public String successcode;
@SerializedName("successMsg")
public String successmsg;
@SerializedName("data")
public Data data;

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 Data getData() {
return data;
}

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


}
This is the class i made for the json response for setting getters and setters.

Answer

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