‫Hello, my friends, and welcome back.
‫In order to use retrofit and implement it correctly, we need to create three things the data classes,
‫the API interface, and the retrofit instance.
‫In this lesson, we will start creating the data classes.
‫The Pojo.
‫Plain old Java Objects.
‫I'm going to use a website called Json, Placeholder dot type I code.com.
‫This will provide us with a free fake API for testing and prototyping.
‫So if we scroll down we can get links for a Json file that we will use in order to test our application
‫so we can use the routes, the get post put patch delete.
‫These are the Http methods that we're going to learn in the next videos and Json placeholders comes
‫with a set of six common resources post comments, albums, photos to and users.
‫In this application we're going to use the album.
‫So if I open the albums, you can see here the Json form.
‫This is the file, the Json file that we're going to.
‫Parse and transform this text into Json objects.
‫So.
‫If you notice here, this is a key.
‫This is the value.
‫This is a key.
‫This is a value.
‫This is a key.
‫This is a value.
‫You can see this is an object with curly braces.
‫And this is another object because separated by comma.
‫And if you notice that this is an array that contains.
‫We scroll down, we can see that there is 100 object, 100 Json object inside one array.
‫So we're going to fetch all of these data, all of these objects and display them into our Kotlin application.
‫Okay.
‫This is our mission.
‫I want from you to understand that we're going to create the data classes in Android studio.
‫I will show you through this course the the three types or the different forms.
‫I told you that I can create it manually representing all of this data in the similar format.
‫For example, this is a key string and this is a key of type and this is a value of type string.
‫These are numbers, so I represent them into integer variables.
‫I can create these manually or I can use a.
‫A website that transforms Json to to pojo so I can use Json.
‫Jason to pojo.
‫I can go to this website.
‫This is the famous website to do this these things so I can generate plain old Java objects from Json
‫or Json schema, or I can go here and go to the plugins and install a plugin that transform our Json
‫file into objects.
‫Don't worry, I will show you all these three methods.
‫So I will start with the basic one and I prefer learning it because later on you will understand why
‫we create this class.
‫So I will create new Kotlin class, I will create it and I name it as if I go back to here.
‫This is for albums, so I will create this name of the class album.
‫Create this class and.
‫As you learn before, the model classes should be a data class.
‫So this data class will represent for us as a model.
‫Okay.
‫So.
‫In this class, I need to create three things.
‫If I go back to this Json file, this is the object I need to create similar variables that represent
‫this similar key value pairs.
‫So if I look here, this is a.
‫An integer.
‫This is an integer.
‫And this is a string.
‫I don't need to fetch the keys.
‫I need to fetch the data.
‫So the values.
‫So I need to create three variables.
‫Val id it's of type integer.
‫Val.
‫Title or I can fetch them like this.
‫The user ID.
‫User ID.
‫It's of type integer and Val.
‫Title.
‫It's of type string.
‫Okay.
‫These are the variables of the Json object.
‫The key value.
‫Key value.
‫The key.
‫Of course, it's a string.
‫The value here, an integer integer and a string.
‫So for that I have created integer, integer and string.
‫Now I want to add an annotation here that it's called Serialize.
‫Name.
‫Syria.
‫List name.
‫It's of.
‫A Json package, Json package, notations, dot serialized name.
‫And this is the ID.
‫I will lecture about this annotation.
‫Don't worry.
‫But I want from you to follow me now.
‫The user ID and serialized name.
‫Title.
‫The ad serialized name at an annotation can be used to serialize a field with a different name instead
‫of the actual field name.
‫We can provide the expected serialized name as an annotation attribute.
‫Json can make sure to read or write a field with the provided name.
‫So for example, if I want to fetch the data later on and display the ID as ID so I can remove the serialized
‫name from here.
‫Also, if I'm going to use the user id similar to user ID, so I can also remove the serialized name.
‫But if I want to display the title as name, so I should mention the serialized name and notation here.
‫So the Json will create the field name, not the field title, but it will assign it with the same value
‫for the for the received title.
‫So if you want to change the name of the key from here.
‫So if I'm fetching the data, I'm getting this value, I will assign it to the key name and not the
‫key title if I'm going to use the serialized name.
‫So whenever you use the same, the same name you can and you cannot remove or add the serialized name.
‫So I can create this album item without serialized name because I'm going because in this exercise I'm
‫using the same name as the variable that I have created.
‫Okay?
‫And and it's very important because I am getting the same element, the same keys from here.
‫So the value of this annotation is used as a name when serializing and deserializing this object.
‫So congratulations, guys.
‫We've created our.
‫Data class.
‫This is the first step in retrofit.
‫So these are the data classes.
‫The poacher.