Fetching Movies List From API using Volley Library in Android Studio (2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey there welcome back to coding stuff and in this video we'll pass json movie list from api so this is the json file that we have and i will provide this in the description box as well so we'll be creating application like this we have the movie list so if i hit on the movie so it will take us to the detail activity where we'll display one image the poster of the movie and then title and the rating and the overview so if you want to connect with me on instagram you can follow me at the id coding stuff070 okay so let's start to create the app so i have created this project and i have added one detailed activity so this is empty right now and also i have added implemented the activity detail and in here we just have one image view one text title and rating and description and also i have added the item layout for holding the each item so it contains an image view with width 165 and height as 145 dp and then we title rating and overview so if we click on this card or this whole card it will take us to the activity detail and the image width and height will expand okay and also the let me show you the overview so the overview takes to which is 200 dp and we have set the input type as the text multiline and also have added the max line that is three and this size is 15 db but in the activity detail but uh you can see there is no max lines of thing because we want to show it a full overview or full description so first of all we'll add a glide and volley library dependency so just search volley library dependency and hit on the first link and also the glide so we'll just copy this implementation and i will paste it in the build.gradle app level okay over here so ctrl v and also this github glide library so i'll just copy the implementation ctrl c now i can just close these tabs so i'll paste it over here hit on the sync now so it is synced now i can just close this and i will show the json file so this is the json file it it contains one array and inside that we have multiple objects of holding the id rating title and the poster of the image as well so i will provide this json file in the description box so so you can use this json file so now i'll just copy this and so search for json generator and hit on this link json.generator.com and i'll just select this all ctrl a and i will delete it and i will paste it over json file and then hit on the generate upload json to server copy json file url and i'll just paste it over here as you can see we have one array as i have mentioned all earlier and then we have this rating title overview and the poster when we have multiple things like like genre action adventure animation science fiction and all that you can also retrieve them as well if you want but we'll just retrieve the rating title over and the poster okay so i'll just minimize this and i will open android studio okay so the first thing that will do here is to add the internet permission so users permission and android permission internet in your manifest file so now i'll just close this so we got the item activity detail i'll just close this for a moment and this one also okay so in the main activity first we'll add the recyclable to our layout so here we got it i'll just constraint it originally in parent and vertical and parent width and height well which odp match constraint so the cycle of view the id and we'll just collect it over here private recycler view recycler view it also will require the request to so private request queue request queue so recycle view equals to find u by id i dot id dot is cycloview recyclable dot set has fixed size so true recycler view dot set layout manager and you will pass new linear layout manager and the context so this and later i will create adapter and we'll just select this with the recycler view okay so first of all we'll create one model class so new java class and let's name it as a movie so we will have three or four variables so first will be private string and this will be title poster and i guess overview and we create one double variable so private double and this will be rating so now we'll create one constructor of this class so public movie and we'll pass this parameter so title string poster string overview and double rating now we'll just save this so this sorry okay so now i'll just right click over here hit on generate create gators and select all of this okay so that's it for the movie class now we need to create one adapter class so click on new java class and let's name it as a movie adapter and this will extends to recyclable so recyclerview dot adapter and here we need to pass the inner class so first of all we'll just create it so public class and we sorry hold on this extends to recycle view dot view holder and we'll press alt enter create constructor and then we'll pass that class over here so movie holder alt enter to implement methods okay so we'll create some variables here so private context context and then also we'll create one list and we'll pass movie type over here movie list and then we'll create constructor of this class as well so public movie adapter and we'll pass the context context and the list so movies okay so this dot context equals to context and then movie list close to movies okay so now in the gate item count you guys movie list dot size okay so in the inner class we'll just collect all the ids of this widgets so we have the image view let's name it as imagine or poster and then we have three textures so first is the title and then overview and rating okay so now we'll just find their id so image view equals to item view dot find view id dot id dot image view okay so make sure you find all the correct ids or it will throw the null pointer exception so now in the oncreate view folder we'll create one view so view we will request to layout inflator.com and here we'll pass in the context dot inflate r dot layout dot i thumb and parent and then the false attached to root return new movie folder and here we need to parallel db view okay so in the on bind view holder first of all we'll create one instance of our movie class so movie movie equals to the movie list target and here we need to pass the position so now we'll call holder dot reading start say text and here we pass movie dot get retreating so it is showing us error because of its double value so we'll just convert it to string and then it will happen so holder dot uh title dot set text and then movie dot gate title holder dot overview equals to sorry dot set text and here we need to pass movie dot git overview and then the last one we'll use glide to load the poster so glide with the past context dot load and movie dot get poster start into holder dot image view and then semicolon in the on bind view holder will come back later because we need to pass this data in the detailed activity so we will do that later so in the main activity to instantiate this request queue will create another class so that will be wally singleton have already explained uh what is wally singleton and if you want to check that video link is in the card and also in the description box so basically wally singleton uh singleton patterns means creating only one instance not multiple so name it as a volley single turn so in here we'll create private uh request queue variable so request queue and we'll name it as a request queue then private static variable so that will be volley single turn and instance then we need to create constructor of this class which will be private so from the outside we cannot create instance of this class and here we need to pass the context context and here we'll initialize our request queue so request queue equals to volley dot new request and here we need to pass this context dot get application context so this request queue will available throughout the application and to create an instance of this only singleton class we need another public method so public and this will be static and synchronized as well so synchronize basically means a thread shape only one thread can access at a time so public static synchronize and then leave the class name so sorry wally singleton get an instance and here we need to pass context of context context and in this class first we'll check if the instance is already created so if instance is equal to null so if it is not created then we'll create the new instance so instance equals to new only singleton will call the constructor of this class and we'll pass in the context okay so if the instance is already created then we'll just return the instance so if the instance is not created will create new and if the instance is already created it will just return the previously created instance then we'll create one public class that will return the request queue so public request queue get request you and then this will this will just return the request queue okay so in the main activity over here we'll write sorry uh the request queue equals to only singleton class dot get m instance and here we need to pass the context so this dot greater quest queue it will return the request queue over here so now what we'll do we'll create one method to load or to fetch the data so patch movies semicolon so just click on the right bulb create that method over here so first of all we'll create one url so string url equals to quotes and same column so we'll just copy this link that we have created using json generator tool and i will paste it over here and make sure you write https over here so as you can see we have array not the object at the start so we need to request for json array so we'll write json array request and we'll name it as json array request new json array request and the first parameter that we need to pass is request dot method sorry dot get and then the url so url and will pass well after that and then we need to pass the response listener so new sorry response listener and put semicolon then new error listener and semicolon so in the error list i will just display one toast error dot get message so in this response uh let me show you we have this complete uh objects so we need to iterate through all this object and we need to retrieve the rating title overview and the poster so for that we'll use for loop in here so far and i i will start from zero second column i should be led stand response dot length and then i plus plus so now we are in the each object so inside the array we have json object so we write json object json object equals to response dot get object and here we need to pass i so same column so this is showing us error because of try catch so surround with try and catch and this will happen so from the json object so it means uh it is over here at i is equal to zero we need this rating and title and overview so we'll create some variables string title equals to json object dot get string and title key and then string overview goes to json object dot get string i guess it is overview we'll check it so the title overview and the poster so string poster equals to json object dot get string and here we need to pass booster and then we need to create one double variable so double and this will be rating equals to json object third grid double and here will pass rating as a key and then semicolon okay now uh we need to create one list so private list and here we need to pass movie type so movie and we'll name the movie list and we'll initialize it over here so maybe list equals to new arraylist and then semicolon okay so over here what we'll do we'll create object of movie class so movie let's name it as a movie equals to new movie and here we need to pass this title and post away overview and the rating okay and then we'll call our list movie list dot add and we'll pass this movie over there and after this after the catch statement i will create adapter offer class so movie adapter adapter equals to new movie adapter and here we need to pass the context so main activity dot this comma the list so movie list and then semicolon so we'll call our recycler view and we'll just set the adapter so dot set adapter has this adapter and we need to add this json array request to request queue so after this request queue dot add and here we need to pass json array request and then semicolon okay so make sure you connect your own device because sometime your emulator doesn't connect to the internet and it will not load the data so i have connected my device now i'll just hit on this run button and it will install the app so it is installed and as you can see it loads the data so spider-man and everything is loaded and you can see we just have three lines the description or overview so as you can see uh the first item is spiderman as you can see the title and the second is paid cemetery kind of thing so in our app as you can see spiderman at the top and then this and the third is avengers as you can see it's working so now uh what we need to do we need to add on key listener to each item and then we'll pass that data to detail activity so i'll just minimize this before uh we need to add id to our constraint layout we'll provide id to it so that will be main layout okay so in here we'll create one constraint layout let's name it as a constraint layout and we'll find its id so constraint layout equals to itemview dot find view by d i dot id dot main layout and then semicolon okay so over here we'll call a holder dot constraint layout and we'll add on key listener to it new on click list now and then we need to create one intent so intent intent equals to new intent and here we need to pass the context and then the detailed activity so we tell activity dot class now we'll create one bundle to pass some data bundle bundle equals to new bundle so bundle dot put string and this will be key will be title and value will be movie dot get title i'll just duplicate this two times and then it will be overview i'll get overview and then the poster url so poster movie dot get poster okay then we need to pass the double value so put double key will be rating movie dot get poster sorry i get rating okay and then we'll use this intent and put extras then we'll pass the bundle and then we'll call context dot start activity and we'll pass this intent okay so we have passed this data from the adapter then we need to receive it in the detailed activity so i'll just close all of these things right now so in the detail activity we'll create one bundle and bundle equals to get intent dot create extras and then semicolon first of all we'll just create collect all the things that we have sent so i'm title name it and bundle dot get string so key is title sorry first duplicate so um poster and the key is poster and then overview and overview then double i'm writing equals to bundle.kit double and here we need to has the rating key okay so let me just check again the keys okay they are fine now we need to collect ids of the widgets from here first of all we'll have the imageview so image view and let's name it as image you find it by id so we have finded all the ids of four widgets as you can see one two three four now we'll just set this value to this widgets first of all we have the image also we'll use glide library so glide with as the context as this then dot load and this will be mpost dot into and we'll pass the image view then we have the rating tv so dot set text and you need to pass the rating book as the m rating third to string and then we have a title tv so title tv dot set text and here we'll pass and title then we have overview tv don't say text and overview and semicolon okay so i think that's it now i'll just hit on the run button and we'll check it's working or not okay so now i will hit on this spiderman so as you can see it redirect me to the detailed activity and here as you can see the title image and the overview so i'll hit on the back and again i will hit on this avenger and game again this one so rating is 9.0 and this is random rating it's not correct so this is working fine okay so that's it for this video make sure you subscribe to channel for upcoming tutorials and thank you for watching
Info
Channel: CodingSTUFF
Views: 5,723
Rating: 4.9389315 out of 5
Keywords: android studio coding tutorial, android studio coding for beginners, android studio coding in flow, android studio coding with harry, android studio coding tutorial hindi, android studio coding with mitch, android studio coding for login page, android studio coding pdf, belajar coding android studio, android studio coding cafe, cara coding android studio, navigation drawer android studio coding in flow, android studio coding for calculator, coding in flow android studio
Id: r5TmVDdFgKE
Channel Id: undefined
Length: 26min 39sec (1599 seconds)
Published: Wed Feb 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.