Reputation: 1787
I want to read a JSON array with Retrofit 2, this is my JSON sample:
{
"id":"58",
"p":"4297f44b13955235245b2497399d7a",
"name":"0634063306cc06340634063306cc",
"contacts" : [
{
"id":"1",
"name":"test1"
},
{
"id":"2",
"name":"test2"
},
{
"id":"3",
"name":"test3"
},
{
"id":"4",
"name":"test4"
},
{
"id":"5",
"name":"test5"
}
]
}
It has some objects and an array with the name of contacts
.
This is my code for reading the objects:
public interface UserLogin {
@GET("/getLogin3.php")
Call<UserItems>getUser(@Query("e") String user, @Query("p")String pass);
}
public interface UserLogin {
@GET("/getLogin3.php")
Call<UserItems>getUser(@Query("e") String user, @Query("p")String pass);
@GET("/getLogin3.php")
Call<List<SimpleItem>>getItems();
}
these are my contact and contacts class as you can see below
public class Contact {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Contact withId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Contact withName(String name) {
this.name = name;
return this;
}
}
public class Contacts {
@SerializedName("contacts")
@Expose
private List<Contact> contacts = null;
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
public Contacts withContacts(List<Contact> contacts) {
this.contacts = contacts;
return this;
}
}
activity :
Call<List<Contacts>>rows=connection.getItems();
rows.enqueue(new Callback<List<Contacts>>() {
@Override
public void onResponse(Call<List<Contacts>> call, Response<List<Contacts>> response) {
List<Contact>contacts=response.body().getContacts();
}
@Override
public void onFailure(Call<List<Contacts>> call, Throwable t) {
}
});
I have call in my activity like this, but it doesn't find the getContacts()
method.
Upvotes: 1
Views: 78
Reputation: 39
https://github.com/robohorse/RoboPOJOGenerator
Intellij Idea, Android Studio plugin for JSON to POJO conversion.
Generate Java and Kotlin POJO files from JSON: GSON, AutoValue, Logan Square, FastJSON, Jackson, Moshi, empty annotations template. Supports: primitive types, multiple inner JSONArrays.
Its is fast way to create pojo
Upvotes: 0
Reputation: 1550
Do it like this:
Contact
public class Contact {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
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;
}
}
SimpleItem
public class SimpleItem {
@SerializedName("id")
@Expose
private String id;
@SerializedName("p")
@Expose
private String p;
@SerializedName("name")
@Expose
private String name;
@SerializedName("contacts")
@Expose
private List<Contact> contacts = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getP() {
return p;
}
public void setP(String p) {
this.p = p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
}
interface
@GET("/getLogin3.php")
Call<SimpleItem>getItems();
Retrofit Call
Call<SimpleItem>rows=connection.getItems();
rows.enqueue(new Callback<SimpleItem>() {
@Override
public void onResponse(Call<SimpleItem> call, Response<SimpleItem> response) {
SimpleItem simpleItem = response.body();
List<Contact>contacts=simpleItem.getContacts();
}
@Override
public void onFailure(Call<SimpleItem> call, Throwable t) {
}
});
Upvotes: 1
Reputation: 456
your UserItems
class should be like this :
package com.example;
public class Contact {
private String id;
private String name;
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;
}
}
-----------------------------------com.example.UserItems.java-----------------------------------
package com.example;
import java.util.List;
public class UserItems {
private String id;
private String p;
private String name;
private List<Contact> contacts = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getP() {
return p;
}
public void setP(String p) {
this.p = p;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Contact> getContacts() {
return contacts;
}
public void setContacts(List<Contact> contacts) {
this.contacts = contacts;
}
}
Upvotes: 0