Varun D
Varun D

Reputation: 15

Is this the right approach to Retrofit POJO without get/set-ters?

Introduction

My code is working fine. I am curious to know if this method is alright for this particular use case as I could not find an example for Retrofit that does not use GSON or other JSON parsing libs but just uses a POJO to hold data.

(Note, I have been trying to format my code for a while! Sorry about that)

For this example, I request data from TMDB's API: .../3/movie/{id}?API_KEY=x

Data Returned in JSON (Reduced. Please focus on "reviews" object)

{
"vote_count":2034,
"reviews":{
    "page":1,
    "results":[
        {
            "id":"55660928c3a3687ad7001db1",
            "author":"Phileas Fogg",
            "content":"Fabulous action movie. Lots of interesting characters. They don't make many movies like this. The whole movie from start to finish was entertaining I'm looking forward to seeing it again. I definitely recommend seeing it.",
            "url":"http://j.mp/1HLTNzT"
        },
        {
            "id":"55732a53925141456e000639",
            "author":"Andres Gomez",
            "content":"Good action movie with a decent script for the genre. The photography is really good too but, in the end, it is quite repeating itself from beginning to end and the stormy OST is exhausting.",
            "url":"http://j.mp/1dUnvpG"
        }
    ],
    "total_pages":1,
    "total_results":2
},
"trailers":{
    "quicktime":[

    ],
    "youtube":[
        {
            "name":"Trailers From Hell",
            "size":"HD",
            "source":"FRDdRto_3SA",
            "type":"Featurette"
        },
        {
            "name":"Trailer 2",
            "size":"HD",
            "source":"jnsgdqppAYA",
            "type":"Trailer"
        },
        {
            "name":"Official Trailer #1",
            "size":"HD",
            "source":"YWNWi-ZWL3c",
            "type":"Trailer"
        }
    ]
}

Method

  1. Created the Retrofit Interface. Wish I could show it here but the formatting is going mental.

  2. This is the curious part. I created a POJO as shown below and it works fine.

    public class TMDBMovieDetailsRetrofitObj {

    // Runtime available in movie/id endpoint public String runtime;

    public String tagline;

    public Reviews reviews;

    public class Reviews{

    public Reviews() { reviews = new Reviews(); }
    
    // To know how many reviews we have got!
    public String total_results;
    
    public List<ReviewResults> results;
    
    public class ReviewResults{
    
        public ReviewResults() {
            results = new ArrayList<>();
        }
    
        public String id;
        public String author;
        public String content;
        public String url;
    }
    

    } }

Did not create getter and setter methods because I have another TMDBMovie object that holds a lot of information and has it's own getter and setter methods.

  1. Basically, I am using this POJO as an intermediately transfer object thingy...

My Questions

Do you think these two points are alright:

  1. This POJO has 3 levels of public nested class that Retrofit handles quite alright.

  2. I do have an TMDBMovie object which has it's own getter and setter methods through which I conduct checks, validations and minor data manipulations. Does this POJO even need getter and setter methods if public variables are doing okay?

Upvotes: 0

Views: 1284

Answers (2)

iagreen
iagreen

Reputation: 32026

  1. No, they three levels of nested class definitions is confusing probably going to add to maintenance issues. Also, the nested classes constructors trying to initialize the outer classes fields is a bit sketchy. Just implement each class one its own --

TMDBMovieDetailsRetrofitObj.java

public class TMDBMovieDetailsRetrofitObj {
    // Runtime available in movie/id endpoint
    public String runtime;
    public String tagline;
    public Reviews reviews;
}

Review.java

public class Reviews {
    // To know how many reviews we have got!
    public String total_results;
    public List<ReviewResults> results;
}

ReviewResults.java

public class ReviewResults {
    public String id;
    public String author;
    public String content;
    public String url;
}
  1. That is a matter of debate and personal preference. For an object that is simply for data that has no other methods and behavior, especially short lived data manipulation objects like yours, skipping getters and setters is often acceptable, especially on Android. Others may have different opinions. If you have a boss or teacher with an opinion on the matter, follow what they want you to do!

Upvotes: 1

novic3
novic3

Reputation: 106

Naming your class variables so that JSON is converted into Java object is a fine idea. However, the classes need not be nested. You could define a List<Result> results and define Result class elsewhere. Hope this is clear.

Upvotes: 1

Related Questions