0

I get such an error: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT, when receive data from server. What can cause such an error? And how to fix it?

JSON which I receive from server:

{
  "group": [
    {
      "name": "Group1",
      "description": "Test Group 1"
    },
    {
      "name": "Group2",
      "description": "Group Name Updated"
    }
  ]
}

Here is API interface:

@GET("groups")
    Observable <List<Group>> getAllGroups(@Header("Authorization") String auth,
                                   @Header("Content-type") String contentType,
                                   @Header("Accept") String accept
                                  );

Method where I receive data:

private void getAllGroups() {
    String credentials = "admin" + ":" + "admin";
    final String basic =
            "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
    String contentType = "application/json";
    String accept = "application/json";
    Subscription subscription = App.service.getAllGroups(basic, contentType, accept)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(groups -> {
                groupList.addAll(groups);
            }, throwable -> {
                Log.e("All group error", String.valueOf(throwable));
            });

    addSubscription(subscription);
}

Class Group:

public class Group {

    private String name;
    private String description;

    public Group(String name, String description) {
        this.name = name;
        this.description = description;
    }

}
1
  • Instead of Observable <List<Group>> getAllGroups use Observable Group getAllGroups as the return type. Commented Nov 8, 2016 at 9:09

1 Answer 1

2

I added class Groups:

public class Groups {

    @SerializedName("group")
    List<Group> group;
}

and changed code in API interface to:

 @GET("groups")
    Observable <Groups> getAllGroups(@Header("Authorization") String auth,
                                   @Header("Content-type") String contentType,
                                   @Header("Accept") String accept
                                  );

And after that app starts work without errors.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.