Getting started with Retrofit | android studio | java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone how's it going and in this video we are going to get started with retrofit uh tool which is uh for available for making the API request and get the responses from it this retrofit is just widely used very popular and you're going to see how easy is that for making the API request let's just create our blank Android Studio project real quick let's create a new project empty project and let's just call it retrofit row the language I just use Java language and hit enter one more thing for making the API request we need API so the for the purpose of this video I'm going to use this website which is requestresponse dot in and it provide you the the dummy apis you can just make the request there is forget there is for post and this is for put delete everything is over there so I think this will be the best one to follow along we are going to use one for get and one for post and we'll just end the video after making the post request our app is now loaded over here the first thing you have to do is open the module level Gradle and come below or you can just click on this GitHub icon and from over here you will going to get the implementation from it or just copy this the version the latest version as so as I'm making this video is 2.9.0 so you can just come to this and just hit implementation and just paste it over here and hit sync now once the work is of over from here next thing you are going to do is get back to your main activity or you can just uh make your front end whatever you want it to look like or however you want it to look like I'm just going to get give it a ID I'll just need to say text View so this text view will going to contain the the data which we'll be going to get as a response from this get request or this APA so we'll just get back to our main activity and I'll create text View text View and just coming in over here I'll say text view is supposed to find view by ID r dot ID dot that's all now in order to make the API request by using retrofit first you need to have a interface so for creating interface it's very easy you just have to say interface and now name the interface whatever you want to you're just free to do so for this request response dot in we are going to get the single user right so this interface will be kind of uh making a request for users so I'll just going to say interface request user interface and in this you will going to have to pass the method so first we are going to learn about get one get method so we'll just have to hit get and now in this you're going to have to pass the path uh the directory where you want to make the request so for this API that path is this one so you're going to have to provide this path so over here you see that you are getting this two three four this is basically the user you want to make the request for instead of making it rigid five or three or two we will going to say it will be uid so that we can when calling this request user interface when calling the method for the response we can pass this uid as as we want to so over here you can come back and create a function which will going to return you the data which is this response data so one thing you need to note over here that it is returning a dictionary like structure and the two fields are there which is data and support and in data there are there is again a dictionary and there is ID email first name last name editor and for support is URL and text so need to create a data class in Java which will going to hold everything of this so let's just get back to our project and in over here you are going to create a new Java class and you can name it whatever you want to I'm just going to say user data now what you have to do structure your data similar to this it has data field and support field so we are going to see if we're going to have data field and support field but inside the data again it is going to have a class of this ID email first name last name so first let's just create it so first I'm going to create a data class in this it has the string of first name so ID email and Avatar similarly we are going to have another class which will be a support class so we are going to say support class and again we are going to say string it has this URL and text make sure you name it as it is so URL and text now for these method for these classes we need to define the data set method you have to hit command n or I guess control n and hit cater and Setter and just make gallery and settle for all of these fields we have defined and similarly for this class get a set method for URL and text let me just minimize these classes now this particular user data class we're going to have the data and support so let's just Define it over here data class data and support class support similarly the getter Setter method for both of them now we have data support and everything is defined well defined now we are going to use this user data class in over here so we are going to define a method let's just say it is for now it is returning void and in this there will be get user and inside this user we are basically going to provide it the uid right so it's a string qid and since this is an interface we don't need to provide it the implementation so over here this your ID is corresponding to this uid but retrofit doesn't know that this uid is this uid so for telling that we are going to use path annotation and in this we are going to say uid so now this signifies that this uid is this your ID and this uid is this uid now for returning part since we are getting the user data as over here so we are going to say call to user data which we have already created over here now once you have defined the interface in over here what you have to do is create a object of retrofit so we are going to say retrofit stratofit new retrofit dot Builder and inside this you are going to have to add the base URL since for now we only added the path to the URL but we didn't provide a base URL so the base URL will be this one so we are going to see this is a base URL and again finally we are going to add the converter Factory so what is this basically I guess is for this is returning the Json but for mapping this Json to this class we are going to need a converter Factory so if we just go to the documentation they are using uh this Json uh converter and you have to provide the implementation for it for using this so we'll just say implementation of this and again the version will be 2.9.0 and hit sync now and you have to get back in over here and just and you just have to say Json converter Factory and hit import and then finally you can say dot build now in order to create the object of this interface we are going to use this retrofit object so we are going to say request user I'm going to say request user is equals to retrofit dot create request user dot class now finally for making the request or calling this method which is a get user we are going to say request user dot get user and pass the user ID we are going to say three now this is going to return us the call to this user data so this call has a method of enqueue and in this you can pass the Callback so basically now when the request will be completed then we are going to get on response or if it is failed we are going to get inside this on failure so I'm just going to uh take this text view which we have already defined in over here we are going to say textview.set text we can just use this T dot get message or if it is successfully completed we are going to say text view dot set text and in this response we have our data so I'm just going to say response dot body so this body is going to return as the user data and inside this user data we have this data class dot data and inside this data class you can access whatever field you want to as you see we have the field of first name last name ID email Avatar so I can just say dot first name now since you can access it anything like this inside this body it is just going to returning you the access to this data and you can just use the dot notation to access any field now if we run our app like this we have successfully made the get request from the API so let's just wait and watch I think our app is going to crash because we forgot to provide it the internet permission so you just have to open the Manifest file and you just have to say use this permission of internet and let's rerun our app now okay so you can see that we are getting Emma and as you see that we were passing user ID S3 and if I just open this API it is just returning me the Json and if I see that for the user 3 we are getting the first name as Emma so we have successfully made the get request so let's proceed further for making the post request post is basically mostly used when you just have to pass the form data or something so uh let's just make a post request for making the post request they are basically two methods and I'm just going to explain you the one other you can just follow the documentation as I have already told you so for making a post request uh over here as you can see the first one is the same way which we have used for making the get request you just have to say at the rate post and for the body you have to create again create a custom data class which we're going to contain all the data fields the form is going to contain so I'm just going to follow up this the other one you can use is a form URL encoded and as a field you can pass them like this so you don't have to define a custom class for it so uh let's get back to our API this is the post API we are going to use so for the data as you see that it is uh having two Fields name and job so what you have to do you don't have to create a separate interface that's the beauty of retrofit you just have to get back in over here and make a post request to the URL let's just copy this URL and paste the path over here now again it is going to return the call to this data which we are going to Define in a bit this will be a response of post so I'm just going to define a class which is response post and I'm going to say post user and it's the body it is going to take the object which is request I am just going to define a class which is request post class so let's just Define these two classes real quick so let's just create a new Java class first will be response post and the other class will be request Post in this basically we are having four Fields as a string name job ID and created it let's say string name job ID created at analysis Define a get a setup for them and for the request post we are going to Define name and job so let's just say string name and job and let's just Define the Constructor for it and let's create the cater set methods also and let's make a request real quick I'm going to comment this this get request and for this I am going to say post user and as you see that it requires the request post object so let's create a new request post and for the name and zip common and for the scene job I'm going to say programmer and again since it is returning call to response post so we are going to enqueue it and again new callback for it and for the failure if any failure occur we are just going to show the user the error whatever error occurred and if it has happened successfully I am going to say response dot body and it is going to going to have ID created at Job name I'm just going to say name and let's run this one more time and let's see if it if it has successfully made a post request yes so we are getting the name as Pawan and you can access any field or work out anything as you wish to by using your own custom API or anything well the purpose of this video has finished you are now uh clean about two matters making get post request uh if you want to make any other requests you can follow the documentation because I think it is very easy and simple to follow along if you still have any queries regarding this you can ask them in down comment section I will see you in the next video till then good bye and keep coding
Info
Channel: ProgrammingHut
Views: 15,319
Rating: undefined out of 5
Keywords: retrofit, android, studio, java, gson, gsonconverterfactory, getting started, begineer, easy to follow, documentation
Id: lz5lPAdA3fQ
Channel Id: undefined
Length: 12min 55sec (775 seconds)
Published: Wed Mar 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.