Android Retrofit POJO model

2019-09-15 04:45发布

问题:

Hi i'm trying to learn android and now implementing the retrofit 1.9 for my rest POST and GET request can somebody help me on how to model given json objects and strings? im very confused on some tutorials I have learned how make a pojo for this json object

{
"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"
            }
    }
    }}]}

Using this model

Contacts.class

public class Contacts {
@SerializedName("contacts")
@Expose
private List<Contact> contacts = new ArrayList<Contact>();

public List<Contact> getContacts() {
    return contacts;
}

public void setContacts(List<Contact> contacts) {
    this.contacts = contacts;
}

and Contact.class for the objects

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;

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;}}

And Calling the list using this on my MainActivity.class

   private void getContacts() {
    final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);

    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
    ContactsAPI api = adapter.create(ContactsAPI.class);
    api.getContacts(new Callback<Contacts>() {
        @Override
        public void success(Contacts contacts, Response response) {
            loading.dismiss();
            List<Contact> contactList = contacts.getContacts();
            String[] items = new String[contactList.size()];

            for (int i = 0; i < contactList.size(); i++) {

                items[i] = contactList.get(i).getName();
            }
            ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.simple_list,R.id.textview, items);
            //Setting adapter to listviesw
            listView.setAdapter(adapter);
        }
        @Override
        public void failure(RetrofitError error) {
        }
    });
}

THen my Question is how should i make a model out of this array Object?

{
"-KNea90tV5nZlkeqxc3Q": {
    "accountName": "Mark Papyrus",
    "accountNumber": "12435656443",
    "accountType": "Peso Savings"
},
"-KNeaPmBoTXV4mQC6cia": {
    "accountName": "Mark Dremeur",
    "accountNumber": "12435656444",
    "accountType": "Peso Checking"
}

i found it confusing how to make models and difference of given json arrays pls guide me thanks.

回答1:

I think me and you are having the same problems but I am stuck so I can help you as far as I can . First you cannot use jsonschema2pojo.org to create the pojo class you need to use hashmap from what I understand "-KNea90tV5nZlkeqxc3Q": is a key but 1 of the classes should be the following ( also the accountype since its a type you can use enum but it would be bit harder to code)

public class Account {




private String accountName;

private String accountNumber;

private String accountType;


public Account() {
                  }




public String getAccountName() {return accountName;}
public void setAccountName(String accountName) {
 this.accountName=accountName;}

// *** repeat for the accountnumber and accountype

 }


回答2:

if the key "KNea90tV5nZlkeqxc3Q" is dynamic and need to capture them, you musto to use a hashmap in your model to catch them correctly.

check this issue it could be useful:

Parse Dynamic Key Json String using Retrofit