[Music]
hey there welcome back to coding stuff
and in this video we'll
see how we can fetch data from api so
if you have any web services and you
want to access data from it
so we use our libraries like retrofit or
volley
to fetch the data from api and in the in
this video we'll use a retrofit library
and in the previous videos uh we have
used volley library
to fetch the data so if you haven't
watched that video
link is in the card and also it is in
the description box
so go and check out so uh retrofit has
five inbuilt methods like gate post put
patch and delete and i will upload
videos for each
method so now we will see difference
between
volley and retrofit okay so this is the
post i have posted
this post on instagram so if you haven't
follow me on the instagram
link is in the description box do follow
okay
so retrofit versus wally so the main
advantage
with retrofit is the first point it
handles
json parsing by itself and volley
requires some extra code for
passing the json so you will get idea
about this when you will watch
volley tutorials but later while writing
the code i will explain you this
okay then second point a retro retrofit
does not support prioritization of
request
and wally supports prioritization and
again a major difference retrofit has no
built-in
image loading support we need to use
glide or picasso
to load the images from url and the
volley has built-in image loading
support
retrofit and volley both support request
cancellation
and retrofit has no retry policy where
wally has
a retry policy and retrofit does not
support caching
while volley supports caching okay so
this is the difference between retrofit
and volley library
generally to face the data from api
retrofit is used
okay so now so i have created one empty
project
and i have added one layout for each
item so we'll have four ticks too it's
so simple uh i just one constraint
layout and inside that
i have card card view and inside that
again we have constraint layout
and inside that we have four text views
okay so
first i will require retrofit library so
just search
retrofit library and click on the first
link
okay hit on this download button and
copy this implementation
and build.gradle app level i'll just
paste it over here
and also will require json library so
just search
json android dependency click on the
first link
and copy this implementation
and i will paste it over here so then
hit on sync now
till then i will visit json placeholder
site
so json placeholder is free fake rest
api
so hit on this link
and navigate to the resource and hit on
this post
okay so this is in the latin but will
not bother about
language because we just need to fetch
this data
so we'll fetching user id id title and
body
all of the pages okay so we'll require
this url
first uh in our
android hit on this
and it says it says are changed to 2.9.0
so hit on that
and then again click on sync now
okay so first of all we'll create one
model class so
i'll name it as post
and in here we'll have four variables so
private
string sorry
so we have user id id title and body
so string user id
id title and
comma body
then i will right click and hit on
generate create getters
select all
okay so now uh we need to create one
interface
so hit on your package and new java
class
and this is interface we'll name it as
json
please holder
they will write call this one coming
from retrofit 2
and as a type will pass list
post
and we'll name the method as a gate post
okay and we need to use annotation of
gate
coming from retrofit 2 again and here we
need to pass
let me show you we need to pass this
post and
rest of the url i'll explain you later
where we need to pass this
so post
will pass it over here okay so this is
the interface uh that's why we
will not implement this method and uh
the major difference that i have talked
earlier handles json passing by itself
so so it will automatically implement
this gate post method
and we'll get all the post okay so in
the main activity what we'll do
first in the activity main we'll just
delete this
text view and i will drag on recycle
over here constraint it horizontally in
parent
and vertical imperative
id will be recycle view
okay so in the main activity we'll just
collect this
so first we'll create a variable of our
recyclable so private recyclerview
recyclerview and then recycle v
equals to find your id r dot id dot
recycler view and then recycle
so it has fixed size and we need to pass
true over here
then recycle view dot set layout manager
new linear layout manager and we need to
pass the context
so this okay so now we'll create one
adapter to set the data
so hit on package and let's name it as a
post adapter
and this will extends to recyclerview
dot adapter
and we need to pass in a class first
we'll create the inner class so public
class
and let's name it as post view
holder
and this will extends to recycler view
dot
view holder
so alt enter and create matching
constructor
then we will pass the post view holder
over here
then alt enter to implement methods and
here we go okay so first of all we'll
create one
list of post
and let's name it as a post list and
again we'll create one context
variables of context context and
we'll create one constructor of this
class as well public post
adapter context it will receive context
and post list
list post sorry
post and let's name it as a post
okay so this dot context
equals to context and post list
equals to post okay then in the gate
item count will pass
post list dot size and
in on create view folder we will create
one view
and we'll inflate delayed so layout
inflator dot from
context dot inflate r dot layout
dot item and parent
attach to root will be false then return
new
pose to view folder and here we need to
pass view
okay so in inner class what we'll do
we'll find all ids of our widgets so in
the
item layout we have four text views
title id body so text view
let's name it as a title we can name it
anything as we want
id and user id and again we have
body okay
so in the constructor will find the id
using the item view
so title equals to item view dot mind
view id
r dot id dot title tv
okay so we have finded all the ids of
our four widgets now we'll just set them
so we'll create one instance of host
class
push post equals to post list
dot k and he will pass position
then we'll call holder dot
id dot set text
host dot get id
then holder dot user id
dot set text post
dot get user id
and then folder dot title
sorry dot set text and then post
dot get title and the last one
holder dot body dot
save text post
dot get body
okay okay so we have done everything
right
now the last thing that we need to do i
will create one retrofit
instance so just write retrofit name it
as a retrofit equals to
new retrofit dot
sorry dot build up and on the next line
dot
base url we need to pass the rest of the
url as
i mentioned earlier so
this url and we'll paste it
over here inside the quotation marks
okay so then add converter factory
so here we need to pass json converter
factory json
okay okay so actually we don't need
this json implementation so i'll just
remove this
but we need retrofit converter
retrofit converter json
and we'll copy this implementation
and i'll paste it over here hit on sync
now
okay so now we will write here json
json converter okay still it is not
identifying so we'll hit on the code
sorry i will hit on
build and rebuild the project
okay so the build has been done now as
you can see
json converter factory start will write
create
and at the last we'll write dot build
okay
so now we need to call our interface so
json
placeholder we'll name it as json
this will equals to retrofit start
create and we need to pass the json
placeholder class so dot class
and then semicolon then again we'll
create one call and inside this will
pass
the list of
post and we'll name it as a call equals
to jsonplaceholder.getpost
and then semicolons so now we'll use
this call variable
and we'll use dot and queue method and
here we need to pass
new callback
list okay so in the on clear
on failure list now we will just display
one toast
and this will be t dot quick message and
one thing i
forget in our manifest file we need to
add
internet permission so user's permission
and internet
okay so in the on response list now
even we have implemented this on failure
listener still we need to check
if this response is successful or not so
if
not equals response dot is successful
then we'll display one toast
with response dot
code and simply will just return from
here
return okay but if it is successful then
we'll create one
list post
let's name it as a post list goes to
response
dot body then we'll create object of our
post adapter so post adapter
limiter post adapter close to new post
adapter
and first thing we need to pass is the
context of main activity dot disk
and then the list so post list and then
semicolon
then what we'll do we'll just set this
with our recycler view
so recycle view dot set adapter and will
pass
post adapter over there okay so
retrofit implements uh this method by
itself so we don't need
uh to add some extra code like we have
done in the
volley library videos so i'll hit on the
run button
okay so it is installed
so as you can see user id is one id is
one and this is title and this is body
user id as one id as two and then
so on okay so again what we can do
here what we can do will some put some
margin of
10 db i guess
okay and then in our post adapter
so we'll concatenate these things id
and colon plus
okay
so now i'll just hit on this run button
okay so now you can see user id as one
id as one then
the title and then body and the rest of
the thing
okay so it is working fine so in this
video we have learned how can use
the gate annotation or implement the
gate method
coming from retrofit library and in the
upcoming videos
we'll use put patch delete and post
methods
so that's it do subscribe and thank you
for watching