ANDROID | JAVA | EASY WAY TO IMPLEMENT RETROFIT2 POST API Call WITH MVVM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to my channel in today's video i'm going to show you how you can make the post api call with the help of retrofit two and we'll use the view model in this video and i'll show you how you can create a user when we are making the post api call so we'll send some parameter then we'll create a user on the server and then will retrieve its response and pass it so let's begin by adding the dependency i have i have already copied some dependencies so we'll add these dependencies in our module level build.gradle file so first dependencies for the retrofit second we need the json converter when we are using the retrofit and this converter will use when will make the api call and this is the life cycle extension this is for the view model and logging interceptor will i'll show you how you can enable the login login in the retrofit so let's sync our project so till the time our gradle is syncing let's create some files so first file we need where we'll be writing our logic for the retron instance so let me give this file name as the retro instance and in this class we'll be using a url we'll making a retrofit api call we'll create a separate function which will return us the retro instance so first thing we need a url so i'm just defining a base url here so that url is the https colon slash slash go rest dot co dot in slash public api and as per the retrofit rule the base url should be end with slash so i'll show you this url so this is the public api so you can see and you need to log in so you can log in inside using the any mechanism so i logged in and this is the api access token so we need this access token to pass with the api if you don't pass this access token then we won't be able to make api call now i'm creating a separate function another static function that will return us the instance of our retrofit two so you can give any name here only thing is like this function should return the retrofit instance so if so i'm just making it as a single turn so i'm defining it as another parameter so when this instance is null then we'll initialize it and we'll pass the base url and we'll add the converter factory so i show you the third dependent second dependency that is the json converter factory will be using here and then we'll call build and we'll simply return this retrofit instance and as i show you in my gradle file dependencies like i'll show you how you can enable the logging in the retrofit to print the logs like request url response so we'll be using http login interceptor and will define its type we'll set the label with this interceptor and then we'll create our http client and stance of this client and we'll simply add this interceptor which we defined just above and now we'll simply attach this okay should be client to our retrofit that is we'll pass the client here and then we'll add it that's it so these three lines can enable the logging inside your retrofit now when we use the retrofit so we need an interface that is a retro service interface i'm giving a name as retro service interface you can give any name inside this interface we define uh http type so as i mentioned we'll make the post api call so i'm defining is http and as i show you we need to pass some headers like the access token so i'm defining some headers here and we need a function we need to define a function and its return type so this is going to be the call and some response type and we'll give a function name as the create user and it this function need a body parameter like some parameters to pass it to the server so that will be certain kind of model class but will complete this url so this post need to complete as a your users and headers we need to pass three headers one is accept that is the accept type is application slash json second header is content type that is again same application slash json and third header we need to pass as the authorization colon bearer and then we'll pass the key or the access token we copied from the portal so we'll directly pass it here so these three headers you need to pass and the body parameter like some params it need to be some certain type of class so we'll create a new class as a user so let's create a new user class and this is simple our model class which will be having some variables like id name this parameter we need to pass to the api call email status and the last parameter is gender i'll simply generate its constructor and the setter getter for this class and with this param we'll already passing it so we need her here response type as well so let me create another data class or the model class i am giving it as a name user response that is also going to have some variables we'll define it like in code string date matter and this user data so you must be thinking like from where i am getting all these information so let me show you this all the api details are defined here so if you see this this is the response it's going to return so we need to pass it create user and then i'll show you how you can that was a post api called user parameter and this is the user um when we are retrieving let's give it a name one one one so this is the one then the response will come so it will look like this so code meta and the data so now let me create a constructor for it as well as the get rent setup for this now we'll pass this class inside our interface here we go and we'll call import and retrofit that's it so our interface is also ready now it's time to create our view model so we need to extend it with the viewmodel we'll define its constructor so let me create its constructor first and now we need to define a live data or mutable live data [Music] which is going to be the user response type because we'll be getting the response holding the response inside this and we can give any name we'll simply initialize inside our constructor now i'll create a separate function which is going to just return this mutable live data now i'm creating another function which is going to make the api call so we need a retro data service instance we will create with the help of retroinstance.get dot get retro instance dot create and then we'll pass our interface name that is the retro service interface dot class and now this retro service interface we have a function that is create user and it need a user object so let's take this user object from the activity and this function should return instance of call and that is the user response type and on this call object we'll call enqueue call dot nq and then we'll register our callback so that is a retrofit callback now we need to call our observer so once we get the response we need to inform our activity so we'll check if response is successful then we'll use this our create new live data dot post value then we'll pass this response over there if it is not successful then we'll pass the null value just to show this there is some error are unable to create a user and similarly if it is failure the api got failure due to some reason they will call null so our viewmodel is also ready now so let's move back to our main activity and define our layout so i'm simply adding one add a text for name another edit text for email and those two parameter will hardcore when we are not going to take as the input from the ui the status and the gender will hard code it for now and we'll add a button on the button click will make the api call oh so layout is ready now so let's go back to our main activity and first we need to handle the create button click so let's retrieve it first so we'll call find view by id so you know with the help of find view by id we get the id of our button we initialize it this way and then we'll set the click listener on this button so this function we need to call so add on click listener then new view on click listener and now we need to call a function on this on click so let's define that function first so i'm giving a name for create new user and here we'll be retrieving the value what user is entered inside our added text so first value will be the name so we'll use the same find view by id and we'll pass the ido4 edit text so similarly we will read the email now we'll build the user object so first parameter is the id we don't need to pass id let's leave it empty second parameter we need a name third is email and fourth parameter is the status so let's hard code it for now last parameter is the gender so i'm also hard coding that now we need to call the viewmodel function so first thing we need to initialize our viewmodel before we call any function of our viewmodel let's call this function from our oncreate function and this create new user from this on button click and now here will be initialize our viewmodel so main activity view model now we'll simply get our observer that is our live data and this on change function will call whenever we call livedata.postvalue so we'll check because we are sending null from there you remember so from viewmodel we are sending null if response is not success if it is success then we'll get the response so in case of null we'll show a toast message with a message we can give any message just to know the user so i'm getting failed to create a new user and here in case of success we'll show the success message so successfully created new user and now let's define this viewmodel as a global variable so that we can access it inside the create new user function and we'll remove it from there so so that we can call create new user and pass this user object sir main activity is also ready now now but before we run our application we need to complete one more step that is our manifest files so we need to add the internet permission now let's run our application and see so our application is launched successfully let me enter some data inside our name name added text and click on create user so you see it has successfully created user so we are successfully able to create a new user let me show you in the logs it's too much logs i think it's all gone let me create uh another user i'm giving one two three let's clear and click on this there we go so you can see this is the post api call we are making these are the parameter headers we are sending and the request and this is the response code 200 and here is the location where data is stored so this is the i'll show you you can see this is the data it's been created inside our database and now here is the response body so you can see this response body inside or logs so let me keep this response in my next video will continue from here next video i will show you how you can make the patch api call and we'll update the same record patch is make to just update the same record and then we'll continue from here in my next video if you have any question you can ask me in the comment section thank you so much for watching you
Info
Channel: LearningWorldz
Views: 908
Rating: undefined out of 5
Keywords:
Id: ytHBE179sZs
Channel Id: undefined
Length: 24min 31sec (1471 seconds)
Published: Mon Sep 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.