How to use the OkHttp library in Android Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] get and post requests using OK http first of all you can come to this URL I'll give the link on the description and scroll down and you can see the Gradle dependency for okay http we can copy this and then here you can see that I have created a new empty project go to Gradle Scripts build Gradle and we have to add the dependency here now again click sing now after synchronizing the project you can close the build Gradle and go to activity main XML file code layout and you can see the file go to the split mode and you can change the pattern layout to linear layout let's add an orientation vertical you can also give a gravity sender now we can add two buttons and a text View so let's remove this from the text View and we can give an ID for the text View you can also give a size for the text View okay now that's all now we can add two buttons you also have to give an ID so I am giving the ID BTN get so on clicking this button we'll be making a get request now let's copy this and create another button let's change the ID of the button to BTN post and also change the text okay now that's all with the UI part now you can go to the main activity Java file here we can create an object for okhdp client and we also need two variables for storing the get and the post urls now you can see that I have created two strings get URL and post URL you can see I have also given the URL so this is a get request and I will show you how the file looks and this is the URL for post request and you can see here get dot PHP and post.php so inside this we are simply echoing the variable that is dollar get and inside the post we are simply echoing the dollar post that's all and I have hosted these files and taken the URL we also need a text view so create a variable we have to initialize the objects so first of all we can do the client equal to new okay http and we can initialize the objects for the button and the text View now we have installed the text view now we have to create the objects for the button and we have to initialize them okay you can see that we have created the objects for two buttons and we haven't sliced the objects so these are the IDS that we used on the activity main.xml file you can see here okay now we can set an own click listener for both the buttons then we have to create functions for making the get and the post request first of all we can do the get request so go outside the own create function and create a function public void get now inside this we'll be making the get request so for that create the request initialize the object no request dot Builder then we can pass the URL so inside this we'll be passing the URL so this is a get request so we'll be passing the get URL so get URL dot build now we can make the call using the okay HTTP client so client dot new call inside that we have to pass the request that we just created Dot enqueue inside this we have to pass the Callback so new callback okay let's scroll down you can see two functions on failure and on response and inside the on response we are getting the response okay so let's see how to access them so inside the own failure we can do error dot print stack trees so you can debug the errors properly and inside the own response function we can show the response string inside the text View so for that we have to create a thread new runnable and inside the Run function we'll be updating the text view so we are already having a text View initialized on the top so text view dot set text and here we can pass the response so response dot body dot string now you can see this in Red so simply click here and type Alt Enter now you can see it is asking you to WAP it inside try catch so simply pop it inside try catch that's all okay now that's done now you can see our get request is almost done and we can make the call to the get request on the button on click listener for the get button okay so call the get function here that's it now we can make another function for the Post okay let's minimize this function okay now here we can make the post request okay let's minimize the on click listen for the button also okay okay so public void post and inside this we'll be passing the request body since we are passing the post variables from the app so we have to create an object for request body let's in slice them with new form body dot Builder and we have to add the data now dot you don't have to give a semicolon here simply hit enter and use the dot operator and call the function add okay now we have to pass the key and the value so the first one will be key and the second one will be the value okay so let's pass the key so for demo we can pass a key like key name okay something like this so if you are passing something like username or password something like that you can use the key name as username okay hope you understand that now here we'll be passing the value and the value let's keep it to demo value okay you can pass as much as you want we are only passing one you want to add more you can simply add more like this okay you can keep on adding okay so we'll be only passing one and dot build now you can give the semicolon okay and after that we have to create the request so just like we did earlier create the object initialize the object and call the Builder okay dot Builder Dot now we have to pass the URL so this one is post URL and we have to pass the data that we just added to the request body so for that we have to use post and pass the request body then dot we can call the build that's all okay now we can give this semicolon now we have to make the new call using the OK HTTP client so for that you can simply copy that from here go to the get function and you can copy this much okay okay this much from here to here okay copy it and we can paste it after this okay now everything is correct I guess okay and we are getting the response here and we are setting it to the UI okay now we have to call this function so scroll to the top and inside the button post we have to call this function foreign to access Internet so go to the Manifest and here we have to add the permission users permission internet close the tab Alt Enter collapse empty tag okay that's all okay now everything is almost complete we can try running the app and check you can see that the app has opened so let's try clicking on the get request button you can see the data has loaded to the text View now we can try clicking on the post request button see now the post request has made and the data is also present here we can try changing the data if you want to understand it much better so here this is our post function let's change the data here to something like some other value let's try running this now we are sending the value code CC with the key name that is key underscore name okay let's try clicking on the post request and you can see the data has changed so if you are wondering what this key name is you can go to the code the PHP code and here inside the post.php file you can see that we are receiving a post variable with the key name that is key underscore name okay so here you can pass something like username name email anything like that okay so this is simply a demo so I am using this okay hope you understand so this is a basic of PHP so you have to understand the basics of PHP okay hope you understand that now that's all hope you understand how to implement get and post because using OK HTTP if you have any doubts feel free to leave a comment I will respond to them and if you like this video give it a thumbs up and subscribe to this channel for more such videos
Info
Channel: Codes Easy
Views: 17,162
Rating: undefined out of 5
Keywords: codeseasy, android okhttp, android okhttp tutorial, android okhttp example, android okhttp post example, android okhttp get request example, android okhttpclient, okhttp android tutorial, android studio tutorial, okhttp, okhttp android studio, okhttp android studio example, okhttp android, okhttp3, okhttp3 tutorial, okhttp3 request example, android okhttp put example, okhttp3 post, okhttp3 android tutorial, okhttp3 request, android okhttp3 example, okhttp3 android example
Id: uSY2RqdBL04
Channel Id: undefined
Length: 13min 2sec (782 seconds)
Published: Mon Dec 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.