1

a few days ago I started using Retrofit instead of OkHTTP in order to comunicate with an API. Everything goes perfect but I have a doubt and I didn't found nothing clear about it.

If a have a JSON response from my API like this

{
"event": {
           "status": "4",
           "participants": "5.455"
         },
"more_data": "string_data",
"even_more_data": 0 
}

That means I should have a POJO structure like this?

public class Event {
    @SerializedName("event")
    public EventData data;

    @SerializedName("more_data")
    public String moreData;

    @SerializedName("even_more_data")
    public int evenMoreData;
} 

public class EventData {

    @SerializedName("status")
    public String eventStatus;

    @SerializedName("participants")
    public String numberOfParticipants;
}

Is there anyway to store all this information in a single POJO?

Thanks

WedgeSparda
  • 1,161
  • 1
  • 15
  • 40
  • Retrofit uses GSON. As said in [documentation](https://sites.google.com/site/gson/gson-user-guide) : "There is no need to use any annotations to indicate a field is to be included for serialization and deserialization. All fields in the current class (and from all super classes) are included by default.". If you want define a different name for the field in the json, of course you need to add the annotation. While if you want skip some fields from serialization/deserialization process you need to define them as `transient` – GVillani82 May 14 '15 at 15:26
  • But that's not what I'm concern. I don't want to define a different name for any field, I want to use a single object to store all the information coming with the JSON response. – WedgeSparda May 14 '15 at 15:43
  • So you do not need to use annotations – GVillani82 May 14 '15 at 15:44
  • Sorry, I don't get it :( – WedgeSparda May 14 '15 at 15:46
  • Looking at your good it seems good. For json you use name like even_more_data, while in the java property you use camelCase. So I think it is good to use the notation – GVillani82 May 14 '15 at 15:50
  • Ok, I got that, but my problem is the data inside the "event". In my class Event, I instanced a EventData object in order to handle the "status" and "participants" fields from the JSON, and what I want is to use a single object (without a EventData Object) to handle all the fields form JSON, and I don't know if there is a way to do that. – WedgeSparda May 14 '15 at 15:55
  • You have to use two classes...but you can declare the second one as inner class – GVillani82 May 14 '15 at 16:00

1 Answers1

0

@Joseph82, it's not always a good idea to use inner classes. There is a real possibility for memory leaks if you are not careful and if you don't know what are you doing. Thus, I will recommend to stick to the two classes approach when using Retrofit (personal opinion).

Here is a link to a stackoverflow discussion on that topic. Check the "Garbage Collection and Non-Static Inner Classes" section

Community
  • 1
  • 1
Todor Kostov
  • 1,789
  • 1
  • 13
  • 20