Android

Retrofit 2.0 library | Fetch JSON API in RecyclerView

retrofit

Display JSON API data in RecyclerView

Youtube Tutorial:

English -> http://bit.ly/2aUu7XR

Bangla -> http://bit.ly/bangla_retrofit

 

What are we going to do in this project??????

We created server side json data in a text file and upload it in our website. Now we want to fetch those json data in our app.

So let’s start out project

  1. Create a project and add below dependency in your android project
2. Add internet Permission in android manifest file
3. Our project structure will look like this

Retrofit library
Project Structure

4. First we will implement our necessary layout for this project

Add this below code in recycler_view_row.xml

 

Add this below code in activity_main.xml file

5. Now we are going to implement this below code in our viewholder class

-> Create a class and name it RecyclerViewHolder. Add this below code

 

-> Add another class named RecyclerViewAdapter. Add this below code

 

6.

We don’t need to create retrofit instance everytime. We just create an instance of retrofit when it is null.

Create a class called ApiClient and add this below code

 

7. Model class is another version of our API data. Model class helps us to receive json data. In model class all variable will be same as we define in our api data.

But you can define variable name as you want. Then you need to use serialiazble before your variable……

Create User class and add this below code

 

8.  Create a interface called APIService. Add this below code

 

9. Add this below code in your MainActivity Class

 

Output

retrofit
server_data_retrofit

 

json api retrofit
Json data in recyclerview

 

Necessary Link:
1) To make json data and see data is valid or not
http://jsonlint.com/

2) To make model class using api data
http://www.jsonschema2pojo.org/

Leave a Reply

Your email address will not be published.

9 − 1 =