1

I've looked at this post and need some clarification.

I have a structure that looks like this:

{
"contacts": [
    {
        "account_id": 3599,
        "created_at": 1427556844,
        "name": "John Smith",
    },
    {
        "account_id": 3599,
        "created_at": 1427155837,
        "name": "Carl Johnson",
    }
  ]
}

And I have created it this way:

public class Contacts {
    @SerializedName("contacts")
    public List<User> contacts;
}

public class User {

    @SerializedName("account_id")
    int accountId;

    @SerializedName("created_at")
    String createdAt;

    @SerializedName("name")
    String name;
}

But when I try to run it with retrofit I get "Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY". According to this post my syntax is correct. But I more into Jake Whartons solution (from the other post mentioned) here, that it actually is a hashmap

Map<String, List<User>>

But changing the contacts object to use Hashmap instead gives me the following error: "Expected BEGIN_ARRAY but was BEGIN_OBJECT". So please help me figure out how to define the objects using retrofit and robospice.

Edited:

I'm using robospice, so I have this:

@Override
public Contacts loadDataFromNetwork() throws Exception {

    final AlertPolicies[] myIncidents = {null};

    return getService().getContacts();
}

And in the activity I have defined in onStart():

spiceManager.execute(contactsRequest, CACHE_KEY, DurationInMillis.ONE_MINUTE, new ContactsRequestListener());

and the Listener like this:

private final class ContactsRequestListener implements RequestListener<Contacts> {

    @Override
    public void onRequestFailure(SpiceException spiceException) {
        if(Constant.DEBUG) Log.d(TAG, "onRequestFailure: " + spiceException.getMessage());
        Toast.makeText(ContactsActivity.this, "failure", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestSuccess(Contacts contacts) {
        if(Constant.DEBUG) Log.d(TAG, "onRequestSuccess");
        Toast.makeText(AlertPoliciesActivity.this, "success", Toast.LENGTH_SHORT).show();

        if(contacts != null) {
            updateContacts(contacts);
        }
    }
}

Contacts is always null and if I look at the response it says "Retrofit Expected BEGIN_OBJECT but was BEGIN_ARRAY" and trying the other way as I explained above gives me the other error.

Community
  • 1
  • 1
peuhse
  • 1,688
  • 2
  • 20
  • 32

2 Answers2

0
HashMap<Integer,User> hash=new HashMap();
@Override
    public void onRequestSuccess(Contacts contacts) {
        if(Constant.DEBUG) Log.d(TAG, "onRequestSuccess");
        Toast.makeText(AlertPoliciesActivity.this, "success", Toast.LENGTH_SHORT).show();

        if(contacts != null) {
            for(int i=0;i<contacts.size();i++){
              hash.put(contacts.contacts.get(i).accountId,contacts.contacts);
            }
        }
    }
msevgi
  • 4,828
  • 2
  • 24
  • 30
0

Thanks, but I think the trick, without having to use callback at all, was actually:

@SerializedName("contacts")
public List<User> contacts;

But I'll keep your hashmap in mind.

peuhse
  • 1,688
  • 2
  • 20
  • 32