当前位置: 首页 > 知识库问答 >
问题:

java.lang.IllegalStateException:应为BEGIN_ARRAY但为BEGIN_OBJECT(改型2)

涂煌
2023-03-14
{
  "group": [
    {
      "name": "Group1",
      "description": "Test Group 1"
    },
    {
      "name": "Group2",
      "description": "Group Name Updated"
    }
  ]
}
@GET("groups")
    Observable <List<Group>> getAllGroups(@Header("Authorization") String auth,
                                   @Header("Content-type") String contentType,
                                   @Header("Accept") String accept
                                  );
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);
}
public class Group {

    private String name;
    private String description;

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

}

共有1个答案

融烨磊
2023-03-14

我添加了类组:

public class Groups {

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

并将API接口中的代码更改为:

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

并且在应用程序开始工作时没有错误。

 类似资料: