Android Tutorial – JSON Parsing using Retrofit – Part 1

Android Tutorial – JSON Parsing using Retrofit – Part 1

Hello to all Retrofiters, Hope you are playing well with OKHTTP. My last post Android JSON Parsing Using okHttp Example with New Material Design Library may useful to all. Now we will see example about Retrofit.

Android Tutorial - JSON Parsing using Retrofit - Part 1 - Pratik Butani

JSON Parsing using Retrofit

Overview:

Retrofit is a type-safe REST client for Android built by Square. The library provides a powerful framework for authenticating and interacting with APIs and sending network requests with OkHttp. See this example to understand how OkHttp works.

This library makes downloading JSON or XML data from a web API fairly straightforward. Once the data is downloaded then it is parsed into a Plain Old Java Object (POJO) which must be defined for each “resource” in the response.

In this example we will create a simple android application to explain the http get request using Retrofit android library. So let’s rock with Retrofit…

Setup:

Make sure to require Internet permissions in your AndroidManifest.xml file:

Add the following to your app/build.gradle file:

In the past, Retrofit relied on the Gson library to serialize and deserialize JSON data. Retrofit 2 now supports many different parsers for processing network response data, including Moshi, a library build by Square for efficient JSON parsing. However, there are a few limitations, so if you are not sure which one to choose, use the Gson converter for now.

Converter Library
Gson com.squareup.retrofit2:converter-gson:2.0.2
Jackson com.squareup.retrofit2:converter-jackson:2.0.2
Moshi com.squareup.retrofit2:converter-moshi:2.0.2
Protobuf com.squareup.retrofit2:converter-protobuf:2.0.2
Wire com.squareup.retrofit2:converter-wire:2.0.2
Simple XML com.squareup.retrofit2:converter-simplexml:2.0.2

Create Java Classes for Resources:

There are two approaches to create Model class. The first way is the manual approach, which requires you to learn how to use the Gson library. The second approach is you can also auto-generate the Java classes you need by capturing the JSON output and using jsonschema2pojo.

We will do parsing JSON from http://pratikbutani.x10.mx/json_data.json which provides following type of data.

Copy and Paste above JSON in http://www.jsonschema2pojo.org/ to Model classes. It will create two models as below (It will created Example and Datum class but I have renamed that class as we can understand properly.):

ContactList.java

Another class is to store individual Contact details.

Contact.java

Creating the Retrofit instance:

To send out network requests to an API, we need to use the Retrofit builder class and specify the base URL for the service.

RetroClient.java

Define the Endpoints:

With Retrofit 2, endpoints are defined inside of an interface using special retrofit annotations to encode details about the parameters and request method. In addition, the return value is always a parameterized Call<T> object such as Call<ContactList>.

If you do not need any type-specific response, you can specify return value as simply Call<ResponseBody>.

For instance, the interface defines each endpoint in the following way:

Notice that each endpoint specifies an annotation of the HTTP method (GET, POST, etc.) and method that will be used to dispatch the network call. Note that the parameters of this method can also have special annotations:

Annotation Description
@Path variable substitution for the API endpoint (i.e. username will be swapped for {username} in the URL endpoint).
@Query specifies the query key name with the value of the annotated parameter.
@Body payload for the POST call (serialized from a Java object to a JSON string)
@Header specifies the header with the value of the annotated parameter

Creating View and Adapter for one Row:

Now we will create one layout_row_view.xml file for the row.

It will be bind through adapter that is MyContactAdapter.java:

Now finally I will send request from our MainActivity, Lets see How we will send request and get the data.

MainActivity.java

It has following view:

activity_main.xml

It will send request and get/parse JSON when you click on FloatingActionButton.

Lets look output:

 

You can Download Demo from: Retrofit_Example_Part_1

Hope you will like and Enjoyed it. Keep Sharing. Keep Learning.

Page Views (32901)

42200 Total Views 137 Views Today
Support with Your Share:

About pratikbutani

Enthusiastic Android Application Developer, Speaker at Google Developer Group - Rajkot, All time Learner of new things, Googler, Eager to Help IT Peoples. Master of all, Jack of ONE.

Comment Your Suggestion or Appreciation:

Loading Facebook Comments ...

2 Comments

  1. Pingback: New Learn – December – 2016 – SAIFUL DIARY

  2. Pingback: Using retrofit parse json in android | Site Title

Leave a Reply

Your email address will not be published. Required fields are marked *