0

I am parsing the API using retrofit but it is giving on response from onFailure. I have tried every possible solution I researched but the result is still the same. My Api looks as follows:

{
"status": 200,
"data": [
    {
        "club_id": 1,
        "club_name": "XYZ",
        "member_info": {
            "title": "ABC",
            "image": "",
            "position": "",
            "description": null,
            "email": "unknown @gmail.com",
            "landline": "000000",
            "mobile": "00000000",
            "website": "unknown",
            "address": "unknown",
            "blood_group": "unknown blood_group"
        }
     } // edited ( "}" was missing)
  ]
}

My Model class looks like as follows:

public class SerialDTO{
@SerializedName("data")
private ArrayList<SerialData> data;
public ArrayList<SerialData> getData() {
    return data;
}
public void setData(ArrayList<SerialData> data) {
    this.data = data;
}
 }

Then I have implemented the data member as follows:

public class SerialData implements Parcelable {

@SerializedName("club_id")
String club_id;
@SerializedName("club_name")
String club_name;
@SerializedName("member_info")
private ArrayList<Serial> serial;


public String getClub_id() {
    return club_id;
}

public void setClub_id(String club_id) {
    this.club_id = club_id;
}

public String getClub_name() {
    return club_name;
}

public void setClub_name(String club_name) {
    this.club_name = club_name;
}

public ArrayList<Serial> getSerial() {
    return serial;
}

public void setSerial(ArrayList<Serial> serial) {
    this.serial = serial;
}

protected SerialData(Parcel in) {
    club_id = in.readString();
    club_name = in.readString();
    serial = in.createTypedArrayList(Serial.CREATOR);
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(club_id);
    dest.writeString(club_name);
    dest.writeTypedList(serial);
}

@Override
public int describeContents() {
    return 0;
}

public static final Creator<SerialData> CREATOR = new Creator<SerialData>() {
    @Override
    public SerialData createFromParcel(Parcel in) {
        return new SerialData(in);
    }

    @Override
    public SerialData[] newArray(int size) {
        return new SerialData[size];
    }
};
}

My member info DTO is as follows:

public class Serial implements Parcelable {

@SerializedName("title")
String title;
@SerializedName("image")
String image;
@SerializedName("position")
String position;
@SerializedName("description")
String description;
@SerializedName("email")
String email;
@SerializedName("landline")
String landline;
@SerializedName("mobile")
String mobile;
@SerializedName("website")
String website;
@SerializedName("address")
String address;
@SerializedName("blood_group")
String bloodGroup;


public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getPosition() {
    return position;
}

public void setPosition(String position) {
    this.position = position;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getEmail() {
    return email;
}

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

public String getLandline() {
    return landline;
}

public void setLandline(String landline) {
    this.landline = landline;
}

public String getMobile() {
    return mobile;
}

public void setMobile(String mobile) {
    this.mobile = mobile;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getBloodGroup() {
    return bloodGroup;
}

public void setBloodGroup(String bloodGroup) {
    this.bloodGroup = bloodGroup;
}

protected Serial(Parcel in) {
    title = in.readString();
    image = in.readString();
    position = in.readString();
    description = in.readString();
    email = in.readString();
    landline = in.readString();
    mobile = in.readString();
    website = in.readString();
    address = in.readString();
    bloodGroup = in.readString();
}

public static final Creator<Serial> CREATOR = new Creator<Serial>() {
    @Override
    public Serial createFromParcel(Parcel in) {
        return new Serial(in);
    }

    @Override
    public Serial[] newArray(int size) {
        return new Serial[size];
    }
};

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(title);
    parcel.writeString(image);
    parcel.writeString(position);
    parcel.writeString(description);
    parcel.writeString(email);
    parcel.writeString(landline);
    parcel.writeString(mobile);
    parcel.writeString(website);
    parcel.writeString(address);
    parcel.writeString(bloodGroup);
}
}

My Api interface:

public interface MembersApi {
@GET("api/family_tree")
Call<SerialDTO> getMyJSON();
}

I have implemented the parsing of API as follows:

 private void fetchMember() {

    MembersApi service = new RetrofitApiClient(getApplicationContext()).createService(MembersApi.class);

    Call<SerialDTO> call = service.getMyJSON();
    call.enqueue(new Callback<SerialDTO>() {
        @Override
        public void onResponse(Call<SerialDTO> call, Response<SerialDTO> response) {

            if (response.isSuccessful()) {

           SerialDTO RealResponse=new SerialDTO();
          ArrayList<SerialData> data=new ArrayList<>();
                RealResponse = response.body();

                data = RealResponse.getData();


                initGridView();

            }
        }

        @Override
        public void onFailure(Call<SerialDTO> call, Throwable t) {
            Log.d("TAG", "Failure" + t);
        }
    });

}
1
  • 1
    You declared "member_info" as an array but it is an object in your response. Commented Aug 31, 2018 at 5:53

3 Answers 3

1

Use This site

for any json related things. Moreover the json you posted is also missing a "}" braces.

Generally your datamodel classes should have been like this:

public class Datum {

    @SerializedName("club_id")
    @Expose
    private Integer clubId;
    @SerializedName("club_name")
    @Expose
    private String clubName;
    @SerializedName("member_info")
    @Expose
    private MemberInfo memberInfo;

    public Integer getClubId() {
    return clubId;
    }

    public void setClubId(Integer clubId) {
    this.clubId = clubId;
    }

    public String getClubName() {
    return clubName;
    }

    public void setClubName(String clubName) {
    this.clubName = clubName;
    }

    public MemberInfo getMemberInfo() {
    return memberInfo;
    }

    public void setMemberInfo(MemberInfo memberInfo) {
    this.memberInfo = memberInfo;
    }

    }

2.

public class Example {

    @SerializedName("status")
    @Expose
    private Integer status;
    @SerializedName("data")
    @Expose
    private List<Datum> data = null;

    public Integer getStatus() {
    return status;
    }

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

    public List<Datum> getData() {
    return data;
    }

    public void setData(List<Datum> data) {
    this.data = data;
    }

    }

3.

public class MemberInfo {

    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("position")
    @Expose
    private String position;
    @SerializedName("description")
    @Expose
    private Object description;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("landline")
    @Expose
    private String landline;
    @SerializedName("mobile")
    @Expose
    private String mobile;
    @SerializedName("website")
    @Expose
    private String website;
    @SerializedName("address")
    @Expose
    private String address;
    @SerializedName("blood_group")
    @Expose
    private String bloodGroup;

    public String getTitle() {
    return title;
    }

    public void setTitle(String title) {
    this.title = title;
    }

    public String getImage() {
    return image;
    }

    public void setImage(String image) {
    this.image = image;
    }

    public String getPosition() {
    return position;
    }

    public void setPosition(String position) {
    this.position = position;
    }

    public Object getDescription() {
    return description;
    }

    public void setDescription(Object description) {
    this.description = description;
    }

    public String getEmail() {
    return email;
    }

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

    public String getLandline() {
    return landline;
    }

    public void setLandline(String landline) {
    this.landline = landline;
    }

    public String getMobile() {
    return mobile;
    }

    public void setMobile(String mobile) {
    this.mobile = mobile;
    }

    public String getWebsite() {
    return website;
    }

    public void setWebsite(String website) {
    this.website = website;
    }

    public String getAddress() {
    return address;
    }

    public void setAddress(String address) {
    this.address = address;
    }

    public String getBloodGroup() {
    return bloodGroup;
    }

    public void setBloodGroup(String bloodGroup) {
    this.bloodGroup = bloodGroup;
    }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to put a braces in api..I have implemented parcelable so as pass arraylist from the intent..I already have tried the method as you described..It works but its difficult to pass arraylist from the intent
1

Didn't you miss the bracer?

{
"status": 200,
"data": [
    {
        "club_id": 1,
        "club_name": "XYZ",
        "member_info": {
            "title": "ABC",
            "image": "",
            "position": "",
            "description": null,
            "email": "unknown @gmail.com",
            "landline": "000000",
            "mobile": "00000000",
            "website": "unknown",
            "address": "unknown",
            "blood_group": "unknown blood_group"
        }
  }//here
  ]
}

Comments

0

I solved it. I was fetching data as an array. I just changed the code in SerialData class as follows:

Previously I have done like this

@SerializedName("member_info")
private ArrayList<Serial> serial;

And solved by doing this:

@SerializedName("member_info")
private Serial serial;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.