filmov
tv
Best Android Studio Tutorial: How to Parse JSON from URL

Показать описание
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.
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:
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.
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:
Oum Saokosal
Photo Credit:
The Creator of Channel Oum Saokosal
Android Json Parsing Tutorial
Комментарии