dependencies used :
compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0'
Json array response can be array response or object response or even a combination of both. See the following three cases
Case 1: Parsing a json array response (OP case)
This case applies to those json responses that take the form [{...} ,{...}]
for example
[ { "id": 3, "username": "jezer", "regid": "oiqwueoiwqueoiwqueoiwq", "url": "http:\/\/192.168.63.175:3000\/users\/3.json" }, . . ]
First create a model class for this array or just go to jsonschema2pojo and automatically generate it as shown below
Contacts.java
public class Contacts { @SerializedName("id") @Expose private Integer id; @SerializedName("username") @Expose private String username; @SerializedName("regid") @Expose private String regid; @SerializedName("url") @Expose private String url; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getRegid() { return regid; } public void setRegid(String regid) { this.regid = regid; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
ContactsInterface
In this case, you should return a list of objects as shown below
public interface ContactsInterface { @GET("/users.json") Call<List<Contacts>> getContacts(); }
Then make a retrofit2 call like the following
Retrofit retrofit = new Retrofit.Builder() .baseUrl("baseurl_here") .addConverterFactory(GsonConverterFactory.create()) .build(); ContactsInterface request = retrofit.create(ContactsInterface.class); Call<List<Contacts>> call = request.getContacts(); call.enqueue(new Callback<List<Contacts>>() { @Override public void onResponse(Call<List<Contacts>> call, Response<List<Contacts>> response) { Toast.makeText(MainActivity.this,response.body().toString(),Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<List<Contacts>> call, Throwable t) { Log.e("Error",t.getMessage()); } });
response.body() will give you a list of objects
YOU CAN CHECK THE FOLLOWING TWO CASE FOR LINK
Case 2: Parsing a json object response
This case applies to those json answers that have the form {..}
for example
{ "id": 3, "username": "jezer", "regid": "oiqwueoiwqueoiwqueoiwq", "url": "http:\/\/192.168.63.175:3000\/users\/3.json" }
Here we have the same object in the example above. Thus, the model class will be the same, but, as in the above example, we do not have an array of these objects - only one separate object, and therefore, we do not need to parse it as a list.
Therefore, make the following changes to object response
public interface ContactsInterface { @GET("/users.json") Call<Contacts> getContacts(); }
Then make a retrofit2 call like the following
Retrofit retrofit = new Retrofit.Builder() .baseUrl("baseurl_here") .addConverterFactory(GsonConverterFactory.create()) .build(); ContactsInterface request = retrofit.create(ContactsInterface.class); Call<Contacts> call = request.getContacts(); call.enqueue(new Callback<Contacts>() { @Override public void onResponse(Call<Contacts> call, Response<Contacts> response) { Toast.makeText(MainActivity.this,response.body().toString(),Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<Contacts> call, Throwable t) { Log.e("Error",t.getMessage()); } });
response.body() will give you an object
You can also check for a typical error while parsing the response of a json object: "begin_array expected, but was begin_object"
Case 3: Parsing a json array inside json object
This case applies to those json responses that take the form {"array_name":[{...} ,{...}]}
for example
{ "contacts": [ { "id": 3, "username": "jezer", "regid": "oiqwueoiwqueoiwqueoiwq", "url": "http:\/\/192.168.63.175:3000\/users\/3.json" } ] }
You will need two model classes, since we have two objects (one outside and one inside the array). Create it as below
ContactWrapper
public class ContactWrapper { @SerializedName("contacts") @Expose private List<Contacts> contacts = null; public List<Contacts> getContacts() { return contacts; } public void setContacts(List<Contacts> contacts) { this.contacts = contacts; } }
You can use the Contacts.java generated above for list objects (generated for case 1)
Therefore, make the following changes to object response
public interface ContactsInterface { @GET("/users.json") Call<ContactWrapper> getContacts(); }
Then make a retrofit2 call like the following
Retrofit retrofit = new Retrofit.Builder() .baseUrl("baseurl_here") .addConverterFactory(GsonConverterFactory.create()) .build(); ContactsInterface request = retrofit.create(ContactsInterface.class); Call<ContactWrapper> call = request.getContacts(); call.enqueue(new Callback<ContactWrapper>() { @Override public void onResponse(Call<ContactWrapper> call, Response<ContactWrapper> response) { Toast.makeText(MainActivity.this,response.body().getContacts().toString(),Toast.LENGTH_SHORT).show(); } @Override public void onFailure(Call<ContactWrapper> call, Throwable t) { Log.e("Error",t.getMessage()); } });
Here the difference from case 1 is that we must use response.body().getContacts() instead of response.body() to get a list of objects.
Some links for the above cases:
case 1: parsing a json array response , case 2: parsing a json object response , mixed: parsing a json array inside another json object