Best Android Studio Tutorial: How to Parse JSON from URL

Published: 09 November 2015
on channel: Oum Saokosal
34,028
124

The Easiest Android JSON Parsing Tutorial: How to Parse JSON from URL (Android Studio) works on any Android Phone. Please subscribe https://youtube.com/oumsaokosal?sub_c...

Android Json Parsing Tutorial: How to Parse JSON from URL (Android Studio): In this video, I will teach how to do JSON Arrray Parsing in Android from URL of a website. Did you know there was no easy way to parse JSON from a URL into objects in Android? Originally we used JSONArray class to parse. Later Google released another lib called GSON. It was fairly useful and easy but you still needed to do a lot of code yourself, which consumed a lot of your time. Even worse, you needed to do the same thing over and over again from one project to another.

Please note that this tutorial may work on your older Samsung Galaxy, LG, Oppo, Huawei, Sony or Nokia Android phones which run Android Operating Systems depended on their API version.

Lately, I discovered a way to make it simpler. Instead of rewriting the code all the time, I created a reusable library. I uploaded the lib on GitHub at https://github.com/kosalgeek/KGJsonCo.... However, I still need GSON to be part of my lib. So you have to download the GSON as well, at http://repo1.maven.org/maven2/com/goo... or you add a Gradle -- compile 'com.google.code.gson:gson:2.2.4' -- in your project.

To use that, first you add a model class that has its properties matching to JSON objects. For example, you have a tbl_post table and 4 attributes (post_id, post_title, post_text, image_url). Then by using json_encode() method in PHP, you should get the same object names in JSON. So, in Android you should create class named Post with 4 properties as well. See the sample code below:

import com.google.gson.annotations.SerializedName;

public class Post {
@SerializedName("post_id ")
public int post_id;
@SerializedName("post_title")
public String post_title;
@SerializedName("post_text")
public String post_text;
@SerializedName("image_url")
public String image_url;
}

The SerializeName annotation is the keyword that helps your JSON objects properties match an Android objects properties. You have to make sure they are the same.

After that, you need to add another library of mine at https://github.com/kosalgeek/generic_... at read the JSON from URL to Android. You can check another YouTube at    • Best Android Studio: How to Connect A...   to see how to connect Android with PHP, MySQL in details.

Finally, what you have to do is to call a one magic line of code:

JsonConverter

This code will parse the JSON text into objects. You can use that ArrayList for your custom ArrayAdapter to display texts and images in a ListView. But in this video, I make it simpler by just using a text list instead. Stay tuned for another awesome video about the custom ArrayAdapter.

Now, you are reaching the end of the Android JSON Parser from URL tutorial. Thanks for watching my video. Don't forget to subscribe for more videos. Like and share it to your friends.

If you want cool videos like this, you can follow me:
   / oumsaokosal  
  / okosal  
https://kosalgeek.com

Oum Saokosal

Photo Credit:
The photo (https://www.flickr.com/photos/sergese...) used in my video cover is copyright (c) by Sergey Galyonkin (https://www.flickr.com/photos/sergese...) made it available under Creative Commons (https://creativecommons.org/licenses/....

The Creator of Channel Oum Saokosal
Android Json Parsing Tutorial


Watch video Best Android Studio Tutorial: How to Parse JSON from URL online without registration, duration hours minute second in high quality. This video was added by user Oum Saokosal 09 November 2015, don't forget to share it with your friends and acquaintances, it has been viewed on our site 34,02 once and liked it 12 people.