Marko Lazarevic
Marko Lazarevic

Reputation: 193

getting nested array of objects, retrofit android

I am using Retrofit android library and Gson converter to get JSON response from my server, this is the response. After receiving the response my app is crashing in onCreate()API. Could anyone suggest me what is wrong with below code?

{
"content": [
    {
        "id": 1,
        "title": "Xrp the standard",
        "desc": "Hodl till you die",
        "createdBy": {
            "id": 1,
            "username": "1",
            "name": "Radovan"
        },
        "creationDateTime": {
            "epochSecond": 1533679200,
            "nano": 0
        },
        "photo": "to_the_moon.jpg",
        "tags": "tagovi",
        "likes": 12,
        "dislikes": 2,
        "comments": [
            {
                "id": 1,
                "title": "Bravo nasi",
                "desc": "Samo sloga Srbina spasava",
                "createdBy": {
                    "id": 2,
                    "username": "",
                    "name": ""
                },
                "creationDateTime": {
                    "epochSecond": 1533679200,
                    "nano": 0
                },
                "likes": 3,
                "dislikes": 4
            }
        ]
    }
],
"page": 0,
"size": 30,
"totalElements": 1,
"totalPages": 1,
"last": true
}

This is my Retro client,

public class RetroClient {

private static final String BASE_URL = "http://192.189.0.27:8080/";

private static Retrofit getRetrofitInstance() {
    return new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    }

public static RestAPI getRestAPI() {
    return getRetrofitInstance().create(RestAPI.class);
}



}

This is my rest api call

@GET("api/posts")
    Call<Example> getPosts();

My Example class

public class Example {

@SerializedName("content")
@Expose
private List<Content> content;
@SerializedName("page")
@Expose
private Integer page;
@SerializedName("size")
@Expose
private Integer size;
@SerializedName("totalElements")
@Expose
private Integer totalElements;
@SerializedName("totalPages")
@Expose
private Integer totalPages;
@SerializedName("last")
@Expose
private Boolean last;

public Example() {
}

public Example(List<Content> content, Integer page, Integer size, Integer totalElements, Integer totalPages, Boolean last) {
    super();
    this.content = content;
    this.page = page;
    this.size = size;
    this.totalElements = totalElements;
    this.totalPages = totalPages;
    this.last = last;
}

//Getters and setters

My Content class (nested array of objects which I need access to, "content" in response)

public class Content {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("desc")
@Expose
private String desc;
@SerializedName("createdBy")
@Expose
private CreatedBy createdBy;
@SerializedName("creationDateTime")
@Expose
private CreationDateTime creationDateTime;
@SerializedName("photo")
@Expose
private String photo;
@SerializedName("tags")
@Expose
private String tags;
@SerializedName("likes")
@Expose
private Integer likes;
@SerializedName("dislikes")
@Expose
private Integer dislikes;
@SerializedName("comments")
@Expose
private List<CommentResponse> comments;

public Content() {
}

public Content(Integer id, String title, String desc, CreatedBy createdBy, CreationDateTime creationDateTime, String photo, String tags, Integer likes, Integer dislikes, List<CommentResponse> comments) {
    super();
    this.id = id;
    this.title = title;
    this.desc = desc;
    this.createdBy = createdBy;
    this.creationDateTime = creationDateTime;
    this.photo = photo;
    this.tags = tags;
    this.likes = likes;
    this.dislikes = dislikes;
    this.comments = comments;
}
//Getters and setters

and finally, my callback method in onCreate

RestAPI rest_api = RetroClient.getRestAPI();
    Call<Example> call = rest_api.getPosts();


    call.enqueue(new Callback<Example>() {
        @Override
        public void onResponse(Call<Example>  call, Response<Example> response) {

            if( response.isSuccessful()) {

              //here, I need to get "content" so I could fill up list in code below,
 //tried almost everything, but activity crashes when created
 postsList = response.body().getContent();


                for(int i = 0; i < postsList.size(); i++) {
                    Content content = postsList.get(i);
                    pAdapter.addPost(content);
                }

            }
            else {
                int sc = response.code();
                switch (sc) {

                }
            }

        }

        @Override
        public void onFailure(Call<Example> call, Throwable t) {
            Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

Upvotes: 0

Views: 3009

Answers (2)

user9923632
user9923632

Reputation:

Your Pojos should implements Parcelable interface

   public class Example implements Parcelable {
//your members
}

Upvotes: 1

Prashanth Verma
Prashanth Verma

Reputation: 588

Add getters and setters for list "content" in Example class and do this "response.body().getContent()" to get the list.

postsList = response.body().getContent();

Upvotes: 1

Related Questions