0

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();
        }
    });
4
  • 1
    what is the problem? Commented Sep 10, 2018 at 12:40
  • post your error log Commented Sep 10, 2018 at 12:41
  • look at call.enqueue, I comment what I need to do Commented Sep 10, 2018 at 12:41
  • imgur.com/a/vzPXqEv Commented Sep 10, 2018 at 12:58

2 Answers 2

1

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

postsList = response.body().getContent();
Sign up to request clarification or add additional context in comments.

6 Comments

I have getters and setters and when I do your solution, I get null pointer exception
can you post the log of crash
what your min sdk version? and on which device are you running your app?
min sdk version is 19, targeted sdk version API 26: Android 8.0, and I am testing on Galaxy J7
try changing your minsdkversion 26 and you should test on device that has sdk version more than or equal to 26
|
1

Your Pojos should implements Parcelable interface

   public class Example implements Parcelable {
//your members
}

3 Comments

still same problem
first generate pojo from [link] (jsonschema2pojo.org) and then make rest call
I did that before everything

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.