Android studio tutorial



hello, my name is oum saokosal. today, i'm going to talk about how to parsejson from a url to objects in android studio. first you need to have json text from url. i will show you how to do that, to convertfrom php to a json text. second one, you need to use my custom libraryi will show you why you need to use my custom


Android studio tutorial, library. the third one, you need gson library fromgoogle. ok! let's jump to the browser.


this is my blog about android. you can read the full tutorial in this blogin this article. the reason that you have to use my customlibrary, because originally you have to use a json array class from google (android). but you can see the example here. when you have a (json) text that representsan object, or maybe a record from a table, so you will have another class, right, likea user class to hold id, username, and password, right? so you have to pass it to the user id, right?


and you have to think about the data typeof id: integer so you have to convert it correctly so it will consume a lot of time. and i originally used it in my previous projectsbut then i realized that it should be a better way thanks to stackoverflow. i found a very good post and question aboutthis and someone wrote a very good answer i use it for my library. so, first you have to use the gson from google. this is my library code so i will not go downto this, instead, i will show you how to use it.


so, first you have to jump to my github.com/kosalgeek. you can find this. you click like this. and then you can download it. so now i already downloaded it and if youwant to know how to set it up in an android project, it's very easy. first, you download this my project and unzipit and download gson from here. and i need to download it as well. so you need these two.


ok? and then the code is very simple. it's just one line of code, you know. let me show you how to do that. but before you do that, you need to have ajson text, right? so, i use my previous project. i have a database called db_client2 and ihave just created a table. it has id, title, text, url and the text inthe text and the other is varchar (255) and i add some text here and the url of an imagefrom the internet.


and then in here, i wrote some connectionhere. that's very simple and connect to the db_client2and the post is just like, okay i included there and then i just checked the format isjson. so, in php there is function called json_encode. it's very simple. you have to create an array. and fetch data from the database and thenconvert it to the json format for you. it's very easy. let's jump to the browser and present ok thisis on json text.


this is hello world nothing's special ok! you want to use the permission internet. like this. then you can connect to a url web and thenhere i don't need this one. it's for menu. you write the code over here. so, you need the two libraries that we have,we have downloaded. okay, this one and unzip this one. so, i need this.


i copied it to somewhere else. json one. now you have this two libraries. so, you go to android. you have to switch to project. yeah and then you have to find the folder,libs. so, it's under app. copy from here and copy json and kg.jsonconvertor. right click here and then paste.


so, right click on each one and then say "add as library" and then this one. so we will take some time to the sync thing. so, you have to wait until the red line disappear. so, to test it, you just follow a method,a class from here. you can see an example here. jsonconverter is my library. if you can see something like this and yousee the import here kosalgeek, we get this, it means it works fine ok? so, now you have the json text in place andyou want to grab that text.


so i have already made a video about how toconnect to, from android to php and mysql. in this video you just type something likethis how to connect android php, you should find my material this one, ok? oum saokosal here. the tutorial i showed how to connect it andyou know like, if you need permission in detail. so, now you grab the text from the url. so, to do that call pos, so you need to anotherlibrary of mine so you go to generic_asynctask. this is the library that went from androidto the php, you know, to get the data from php.


in here, you just need only this one genasync. copy and go to your android project. you have to go back to project again and ifyou paste in libs, where is it? here. and then right click on that file and addlibrary. so, we'll finish, okay. that's great. so, you need postresponseasynctask and youcan call this task. and task.execute.


so you give your url. so, where's your url? architects. in here, ok? copt this and then here. one problem here is the localhost becausenow you're connecting to computer and you run from an emulator. at this day emulator has the local of itselfso, you have to give another name. so that is on an alias for 10.0.2.2 for internalemulator but i use the genymotion.


the number two is changed to number three. there is something here and happy is becauseyou have to make the class implement action respond and you need to alright processfinish. just the result ok? the result that you grab from the url. it means the json will come to this to thismethod. now, let me start the genymotion. if you don't have genymotion, you should pausethe video and download it. it's free and you have to sign in.


so, first, let me just test the text likethis and then the result is the json file, json text. let's run it as you can see, now we get already,the text of a json. it's because of this library. so you don't have to spend your time to writeyour own library, just use mine. and then, now, we get the result as the jsontext. the next thing is to convert the json to anobject. so now we are talking about the post right? we have to create a class that have four propertieslike this, post_id, post_title, post_text


and image_url. so, let's go back to the android perspectiveagain and right click and new class. java class, not activity. just java class. and let's call it post ok? and for this one i use the gson, the gsonlibrary and let's say we have a integer, post_id like this. and we call, serializedname id ok? and then,


public string post_title; public string post_text; and then, public string image_url okay? so, for the serialization, it just means thatwhen you don't have the name exactly as the an object like in database, i'm sorry, injson like this, what you call id like this but in the json, you use the post_id. you have to tell that, ok! now, you have too match the post id to thisone.


so that is the serialization. that is the library from json but you shouldmake it the same, even though you have this one match to this one, but your property nameshould be the same as this one. so, just copy and paste like this and youhave to make sure that this, actually match this. post_text. even small, small letter or capital letter,you have to do it exactly the same. so now it's done you can just close it. now, you use one of my magic line of code,which is this one.


and you just change this one and this oneand this one. now the result is replaced with the json. so now you already get the arraylist of thedata of the json text. so you can place it into the like, listviewok? or maybe some other thing you can make a costumwith you, but let's finish our project by, create a simple listview ok? i go to design and i grab a listview here,yeah this one. let's name it. i call it ivpost.


this one i don't have to use the custom one,because i have another library ok? but just to test it and you want to see, okthe result comes and so what this part of the project i'm not going to make the customlibrary, but just grab the, maybe the text or maybe the title from the of the array. so, i have to look maybe, i have to createanother list of string. and then it's a title. yes? and array. and for each in java, you say for and thensay string, value and then, you get from post.


no, no, because i didn't change this one. i'm sorry. i just let's create a post, yeah and then,every time you get the post, you just like add the title from the post. value.post_title so, now you just change the data from thewhole post but we grab only the title only. so now, we have to create arrayadapter. it's simple one. string.


call it adapter. arrayadapter. this and use the android layout. simple_list_item1 and then title and then i have to get thelist from the xml. i say, lvpost. usually i keep the name like this. it's easier for me to remember. ok! lvpost.


you should, setadapter like and this is in. in the next video, i will show you how tomake a custom list view layout. just stay tuned for my next video but fornow, just use the simple, like a simple list item ok? boom! now you got the mountain and penguin. it's from this one, see? from this one and another one.


Android studio tutorial

please subscribe my video.


to make sure that you get the most updateand i will post another video about a custom list view layout in maybe next week and don'tforget to visit my blog, top12review as well. so for now, i say goodbye and i will see youmaybe next video.


Android studio tutorial Rating: 4.5 Diposkan Oleh: PaduWaras