fetch

JSON DATA FETCHING AND PARSING FROM URL ANDROID STUDIO TUTORIAL | Developing an ANDROID APP 2017



Sharing buttons:

hey guys welcome my name is Abhishek and

it's been a while since I have created

any Android studio tutorial so today I

am here with another Android to do

tutorial in which I will be telling you

how to fetch data from a JSON file from

the internet and use it in your android

app so what this would allow you to you

know make your app dynamic get data from

the server from the internet okay and

you know and use it in your Android apps

okay so it's going to be a pretty good

tutorial so before that I need to tell

you what JSON files are okay what JSON

files are and what are they used for

see we are going to create data files

and I'm going to teach you how to use it

it's kind of a data structure okay and

we are going to use clear JSON files

okay this is a text editor you can use

any text editor you want okay and it's

basically a text format let's just do

this okay so we are going to create a

new file

this is untitled one okay and let's just

try to create a jason okay so let's just

create a JSON file here it's going to be

a simple one okay you just keep a

tension here so first of all open curly

braket okay so let's say we have four

attributes name password contact and

country okay and we are going to fetch

deep so for that Jason for that jason

has to provide these four attributes

lets it create those okay so first of

all we are going to type in name and

then colon in and either end you know

inverted these are in quotes alright so

here let's try to write the word p-value

so my name is Abhishek okay and here it

goes then comma next line password then

colon let's say 1 2 3 comma the contact

: the value which is going to be

literary okay then :

the country then : and we are going to

provide the country which is India okay

now okay so this has been done now

first of all this is a JSON object to

get but we want to provide more than one

object we want to provide more data okay

so what would we do is created na okay

an array of JSON object how would we do

that

see how would we do that in in JSON you

can create an array by providing the box

bracket okay you can see here and a box

bracket here now this becomes a JSON

array and this is our first object okay

so what are we going to do is ctrl T

that put a comma here and you know press

Enter control B + comma press comma

enter control V so three three objects

would be enough for now

okay and I'm going to change the data

okay and okay change the name only or

let's say change the data - okay I'm

just going to type in any random stuff

okay the first one would be more than

enough and okay this is all right

now this object has been made but how do

we know that this thing is correct let's

just ctrl a ctrl C we are going to copy

this okay and I'm going to paste it in

this website Jason link calm it's going

to validate our Jason ctrl V okay and

it's going to validate it so it says

validation which means it's a validation

which means

that we have written it in the correct

way so we wrote it in the correct way

but now we have this in our local file

okay we want to put it on the internet

so that with the check how could we

fetch it so for that obviously the

people who have a server would be doing

that and uploading it to their server

but we can do that because we don't have

any whole thing so what would I suggest

you to do is go to my Jason calm enter

your valid Jason here and click Save

okay so it has uploaded it to its own oh

you know server you can say and it has

saved it to this URL okay now if we

place it here okay so you can see your

Jason here which means that now you can

so now you can access this data from the

URL so you have to keep in you know for

now keep this safe in your text editor

now we don't need this alright

we actually donate it but I'm just going

to create a new file here this is going

to be very important okay so now our

work has been done that the data is

ready for us to fetch from our Android

application may do note that you can

change this data if you have a server

because since we don't have a server we

can't change the data now okay so let's

just go to our Android project this is a

completely clean project here and we are

going to work on the Atlantic so we have

fetching data from the internet we do

want to input this thing which is add a

permission we do want to add a

permission for the app to use the

internet so just go to manifest Android

manifest type in uses permission okay

and then start typing Internet

it would say Android of permission dot

intranet okay and that's what we want

all right this is good

and okay we have added the permission to

now let's try to do the thing so let's

just try to write some code here so here

let's just go to our layout file let's

create a button here fast

okay instead of a textview let's create

a button and change the layout

I don't like a correct layout I would

use a relative layout all right let's

create a button button wrap content wrap

content okay

type in the text which is going to be

click me an ID that they should say okay

click me actually we just need one

button in this whole project so yeah we

could say button okay okay so as this

button has been made and I would add a

text view now

okay it should be match parent and how

it should be wrap under it all that's

the okay

that's all right layout below should be

below the button so that's P it's below

and let's just change the height to

match parent again so now we have the

text this button the text this button

again let's just write hint here good

has it add a bit of margin then giving

the other margin below left a margin

bottom that could that's more than

enough

and add a bit of padding here too that's

just your own personal preference I like

to add the padding in the margin so that

it looks a bit better instead of just a

up text size a bit big to 24 Oh

24s be more than enough okay now one

thing I did want to tell you that then

are you know the data could be more than

this this much of height we should be

adding a scroll view that that would be

better if we add a scroll view that's

very important match parent and match

parent okay and let's just put this

whole thing in it in this scroll view

okay let's say we here and this thing

which is the you know layout below king

should be in this okay so that's pretty

much it for the layout again that has

been made now let's just go to this main

activity and define a button here which

is going to be click and the text field

which is going to be the data okay and

let's just define it here and I haven't

really provided any ID here so let's

just add the ID also let's tape it up

fetch the data good job click equals

button find you by idea I would type

crafted later ID dot button camp it said

something all right

to another one it's the text view find

you by ID r dot ID dot text or fetch the

data good so this has been done now what

we need to do is whenever this button is

clicked we want to perform some tasks

okay so we are going to define this

account click listener click dot set on

click listener new on click listener ok

so whatever I write here is going to get

triggered or executed whenever this

button is clicked which is this button

ok so for that what we need to do is

create a new class because it's going to

be a background process since Android

doesn't allow us to do these background

paths on our main activity thread you

could say the threads are basically

different processes which are working in

Android app and since this background

task so why can't you execute this

background tasks on this UI thread well

this is because if you do it then the

Android UI would be would be frozen and

for that matter of fact the Android app

would crash and that's why Android it

doesn't allow you to do these background

tasks in the main UI thread okay so you

have to do it in a background thread

which is another ok create another

thread so go to me so go to this you

know create a new Java class say fetch

data what am i typing and that and let's

click ok and extend it

from I think dark good and that's just

it needs three parameters type in void

for now okay we would change the monthly

you know define the whole class so

implement the methods if it doesn't

really come here so you can just right

click here it generate implement methods

and click OK alright so this is our

doing background function and we

actually want to do that ok we can also

add override methods which are we need

so we need an override method two which

is on post execute these two methods we

need only these two methods for now

these are the background function this

is beyond post execute so you can do the

you is you can change the UI in on post

execute but you can't do that in here

this is the background thread this is

the UI thread okay so after the veritas

has been done you can change something

in the UI so that's how this works okay

so now what would we need is this URL

okay so let me just type in URL URL

equals new URL and in it I'm going to

paste this URL mmm semicolon here click

here and settles enter to import the

library ok now it says is there is still

an error because we do have to provide a

try and catch around with try and catch

okay you know since if it doesn't happen

what would we want to do okay so this

URL has been created so our data will be

fetched from this URL only and we what

we have on this URL we created that each

item and we do have our JSON on

URL okay so now let's create a request

HTTP or first of all we have to clear

the connection

okay HTTP URL connection and it's going

to be URL dot open connection so we are

opening a connection with it and you

know you are with this address

it says that we have to cast it to the

HTTP URL connection okay now that's good

let's just do one more thing we have to

create an input stream down again so

let's create input string input stream

HTTP URL connection dot get input stream

now what are we going to do is create an

you know bufferedreader

so bufferedreader which is going to read

the data from this input string so

basically what we are going to do is

this is a connection okay this is the

connection the HTTP URL connection there

is a stream which is needed which is

provided in this connection only which

would let you know which would let us to

you know read or write data and we are

getting the input stream from this

connection okay this would let us read

the data okay

so this input string is here now buffer

reader would read the data from this

train equals new buffered reader

okay new input stream reader we have to

provide it an input stream reader okay

and here we have input stream that's

good looks good so now it has been

connected this buffer you know has been

connected to this input stream so it can

now read the data from be input stream

alright so most of the task has been

done now we just have to read down so

what would I do is just go to file

create a string here

line which would have each and every

line okay equals for now while line is

not equal to now we are going to read

the line okay so how would we read it

line equals bufferedreader dot next read

line okay so it would read line a

penalty line and yeah that that's how we

would read the line each and every line

of th JSON file now we would obviously

like to create a string here which is

which would be the unit data the whole

JSON file and let's just do one thing

data equals data plus line okay actually

we could have used actually we could

have used to shrink buffer but okay

that's how that's fine so so we have all

the data the whole JSON file in the data

because it is basically adding the all

the giant forget all the lines are being

added to this data so we got each and

every date and now we have to show it to

the you know to the user how would we do

that we would want to do it from here on

post execute here what we need to do is

what I would be doing is just calling

the text view of this main activity dot

such data whatever the name was and for

that we have to create it static make it

static okay public static

yep so data dot aspect to our data if

you write this this would be great okay

so this has been done I think most of it

up just how to create the object of this

such data here okay so yeah let's just

do that fetch data the process let's

that name is process so that we don't

get confused here equals what new such

data now process dot execute method

that's pretty much it

let's try to run it as you guys can see

now we do have our application here

let's click here yes we did get the data

from here but now you'd be like what the

heck of me shake what kind of info is it

like it has really passed the data but

it looks like this we want to get the

data only we don't want it to be like

this okay so we are going to pass this

data now parse PA RSP okay let's just

parse it how would we do that

we are going to do that in our post

execute method or let's do it in our

doing bagging methodology okay so once

we get the data what are we going to do

here okay let's just define a driven

here JSON array here okay from the data

we are going to define a JSON array just

an array J a equals new JSON array and

provide the data here so now it knows

that it is a JSON

Eric add cash flow so let's just so

let's define a while loop now again

Plus let's just type in a for loop for

int I equals 0 I less than J dot length

I plus plus now what we are going to do

is define a given object now

so each jason object is going to be an

element of this JSON array ok so

basically each just an object each json

object is basically from here to order

lifted this is a JSON object this is

another one this is third one okay so

let's do try it

J OH equals JSON derivative to a dot get

I get a we get we got digit an object

another okay we have to look at it so we

are going to get the values from the

attribute so let's go here and add all

the data to another string let's define

another string so let's just define two

string here again string data part and

string single forest good so now let's

just go here and single part which is

single object single JSON objects in a

path manner is going to be equal to

let's say named Ben :

then plug JSON okay with Jo dot get name

okay so we got the name from here then

plus then

okay so that we go to the next line then

plus again and let's just come here and

copy this basically we are going to copy

this for four times paste it here

test it and fit it and yeah okay get we

got the name password contact and the

country and we are basically trying to

organize this in a part manner so that

in a passage so that we could see it in

a proper manner and you can do it with a

ListView okay and I have made a complete

tutorial on illiterate so see what we

are trying to do here is part the data

in a readable manner okay so that the

user can see the data in a proper manner

and this thing which I'm trying to do is

the silliest thing you could do here

because I'm trying to do this because we

don't have a lot of time here but you

can do this in a proper manner using a

recycler view or a little you now I do

have a tutorial on custom list view you

should watch it and use that to show

your data in a proper list format okay

so the link to that video would be in

the description down below YouTube you

should definitely check that out if you

don't know what custom list view is and

if you want to print this in a proper

manner okay and yeah so check that out

for now we are going to you know pass it

in a string only okay so we got this

strength and you know we are going to

say data parsed is going to be equal to

data bars plus single path so it adds

each object into into this total data

okay and

we are going to change this from data to

data part that's it I hope this would

work and show us the data in a proper

manner let's see what happens now

okay now let's click here so we got it

the name password contact country name

password contact country name password

contact and country I could just go here

after this we could add a /n here too

now if I just run it again click and now

we do have this in a proper manner okay

so what we learned here is what a JSON

file is how to fetch the data from the

internet and Unipart it in a proper way

but still if you want to use the custom

list view the video will be linked in

the description down below and yeah

that's pretty much it and I do have a

whole course on Cortland which is coming

and you know some of the videos that

they have been released a link to that

playlist would be in the description

down below too so you can check that out

Kotlin is a big thing it's going to be a

big thing because Android has officially

announced that Android studio is going

to support Cortland

so yeah you should learn Catalan you can

check that course out it's completely

free link will be in the description the

playlists basically the playlist on my

youtube channel so after you if you did

like this video don't forget to hit that

like and subscribe button and yeah

comment down below what you liked what

you didn't like if you have some

suggestions for me and I'll see you in

my next video