IMPORTANT POINT ::
1.In Retrofit library, Data parse according to POJO classes. If convertor is in GSON then convert your JSON to GSON POJO class.
2. If JSON is nested then multiple POJO Class is generate. In this app, Contact is array name and sub-array is Phone then 3 POJO Class will generate.
i. MainPojo - It is first class where all JSON array and sub-array listed and return POJO type value
ii. Contact - Json array, It's value can retrieve by MainPojo class.
iii. Phone - Json Sub-Array, It's value can retrieve by Contact POJO class.
STEP-1 : AndroidManifest.xml
----------------------------------------------------------------------
STEP-2 : App level Gradle Library
-----------------------------------------------------------------------------
STEP-3 :
ApiInterface.interface
-----------------------------------------------------------------------
-----------------------------------------------------------------------------
STEP-3 : ApiClient
------------------------------------------------------------------------------
STEP-5 : MainActivity.java
-----------------------------------------------------------------------------------
POJO CLASSES
STEP-6 : Movie.java (POJO Class)
-------------------------------------------------------------------------
STEP-7 : Contact.java (POJO Class)
----------------------------------------------------------------------
STEP -8 : Phone.java (POJO Class)
----------------------------------------------------------
STEP-10, Finally JSON
----------------------------------------------------------------
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c203",
"name": "John Wayne",
"email": "john_wayne@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c204",
"name": "Angelina Jolie",
"email": "angelina_jolie@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c205",
"name": "Dido",
"email": "dido@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c206",
"name": "Adele",
"email": "adele@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c207",
"name": "Hugh Jackman",
"email": "hugh_jackman@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c208",
"name": "Will Smith",
"email": "will_smith@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c209",
"name": "Clint Eastwood",
"email": "clint_eastwood@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2010",
"name": "Barack Obama",
"email": "barack_obama@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2011",
"name": "Kate Winslet",
"email": "kate_winslet@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2012",
"name": "Eminem",
"email": "eminem@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
-----------------------------------------------------------------------------------------
22
1.In Retrofit library, Data parse according to POJO classes. If convertor is in GSON then convert your JSON to GSON POJO class.
2. If JSON is nested then multiple POJO Class is generate. In this app, Contact is array name and sub-array is Phone then 3 POJO Class will generate.
i. MainPojo - It is first class where all JSON array and sub-array listed and return POJO type value
ii. Contact - Json array, It's value can retrieve by MainPojo class.
iii. Phone - Json Sub-Array, It's value can retrieve by Contact POJO class.
STEP-1 : AndroidManifest.xml
----------------------------------------------------------------------
<uses-permission android:name="android.permission.INTERNET"/>-----------------------------------------------------------------------
STEP-2 : App level Gradle Library
-----------------------------------------------------------------------------
// retrofit, gsoncompile 'com.google.code.gson:gson:2.8.0'compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'com.squareup.retrofit2:converter-gson:2.0.2'--------------------------------------------------------------------------------
STEP-3 :
ApiInterface.interface
-----------------------------------------------------------------------
import retrofit2.Call;import retrofit2.http.GET; public interface ApiInterface { @GET("/contacts/") Call<Movie> getTopRatedMovies(); }
-----------------------------------------------------------------------------
STEP-3 : ApiClient
------------------------------------------------------------------------------
import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory; public class ApiClient { // public static final String BASE_URL = "http://192.168.1.10"; public static final String BASE_URL = "http://api.androidhive.info"; private static Retrofit retrofit = null; public static Retrofit getClient() { if (retrofit==null) { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); } return retrofit; } }------------------------------------------------------------------------------
STEP-5 : MainActivity.java
-----------------------------------------------------------------------------------
import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.widget.Toast;import java.util.List;import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); apiService.getTopRatedMovies().enqueue(new Callback<Movie>() { @Override public void onResponse(Call<Movie> call, Response<Movie> response) { if(response.isSuccessful()) { // Toast.makeText(MainActivity.this, "OUTPUT : "+response.body().getContacts(), Toast.LENGTH_LONG).show(); Log.d("MainActivity", "posts loaded from API"); List<Contact> contactList = response.body().getContacts(); for(int i = 0; i<contactList.size(); i++) { int statusCode = response.code(); String id = contactList.get(i).getId(); String name = contactList.get(i).getName(); String email = contactList.get(i).getEmail(); String address = contactList.get(i).getAddress(); String gender = contactList.get(i).getGender(); Phone phone = contactList.get(i).getPhone(); String mobile = phone.getMobile(); String home = phone.getHome(); String office = phone.getOffice(); Toast.makeText(MainActivity.this, "OUTPUT : "+statusCode +", "+ id +", "+ name +", "+ email +", "+ address +", "+ gender +", "+ mobile +", "+ home +", "+ office, Toast.LENGTH_LONG).show(); } }else { int statusCode = response.code(); // handle request errors depending on status code } } @Override public void onFailure(Call<Movie> call, Throwable t) { Log.d("MainActivity", "error loading from API"); } }); } }-----------------------------------------------------------------------------------
POJO CLASSES
STEP-6 : Movie.java (POJO Class)
-------------------------------------------------------------------------
import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; import java.util.List; public class Movie { @SerializedName("contacts") @Expose private List<Contact> contacts = null; public List<Contact> getContacts() { return contacts; } public void setContacts(List<Contact> contacts) { this.contacts = contacts; } }-------------------------------------------------------------------------------------
STEP-7 : Contact.java (POJO Class)
----------------------------------------------------------------------
import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; public class Contact { @SerializedName("id") @Expose private String id; @SerializedName("name") @Expose private String name; @SerializedName("email") @Expose private String email; @SerializedName("address") @Expose private String address; @SerializedName("gender") @Expose private String gender; @SerializedName("phone") @Expose private Phone phone; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Phone getPhone() { return phone; } public void setPhone(Phone phone) { this.phone = phone; } }--------------------------------------------------------------------------------------
STEP -8 : Phone.java (POJO Class)
----------------------------------------------------------
import com.google.gson.annotations.Expose;import com.google.gson.annotations.SerializedName; public class Phone { @SerializedName("mobile") @Expose private String mobile; @SerializedName("home") @Expose private String home; @SerializedName("office") @Expose private String office; public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getHome() { return home; } public void setHome(String home) { this.home = home; } public String getOffice() { return office; } public void setOffice(String office) { this.office = office; } }---------------------------------------------------------------------------------------
STEP-10, Finally JSON
----------------------------------------------------------------
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c203",
"name": "John Wayne",
"email": "john_wayne@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c204",
"name": "Angelina Jolie",
"email": "angelina_jolie@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c205",
"name": "Dido",
"email": "dido@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c206",
"name": "Adele",
"email": "adele@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c207",
"name": "Hugh Jackman",
"email": "hugh_jackman@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c208",
"name": "Will Smith",
"email": "will_smith@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c209",
"name": "Clint Eastwood",
"email": "clint_eastwood@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2010",
"name": "Barack Obama",
"email": "barack_obama@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2011",
"name": "Kate Winslet",
"email": "kate_winslet@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2012",
"name": "Eminem",
"email": "eminem@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
-----------------------------------------------------------------------------------------
22
Was helpful.
ReplyDeleteI have one question, how can we access display following info??
"id": "c2011",
"name": "Kate Winslet",
"email": "kate_winslet@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
ayyy LMaooo watch you sayyyyyy
Deleteayyyyyyy
Thaanks!!
ReplyDeleteThis was very helpful.
ReplyDeleteHelpfull
ReplyDeletethankyou that was very helpful
ReplyDeletehttp://igts.co.in/location.json
ReplyDeletehow to display this api data in listview and listitem click open location in map
please write full code
using retrofit
ReplyDelete