Plays2 Plays2 - 1 year ago 155
Java Question

Retrofit expected BEGIN_ARRAY but was BEGIN_OBJECT

I'm working on interacting with my REST API from android via retrofit and RXAndroid, and I've run into a problem.

When I send a GET request to my 'api/users/{id}/{id}/classes', my api responds with a list of the users classes, like so:

"classes": [
"assignments": [
"asstype": 0,
"classs_id": 1,
"desc": "Read",
"due": "9/14/16",
"id": 1
"badge": 0,
"days": "W",
"id": 1,
"location": "W.Claire Harris Textiles 136A",
"name": "STS 1101",
"professor": "Stickel",
"time": "5:00 - 6:15",
"user_id": 1
"assignments": [],
"badge": 1,
"days": "TR",
"id": 2,
"location": "Atrium 201L",
"name": "HIST 2111",
"professor": "Pierce",
"time": "6:30 - 7:45",
"user_id": 1

My interface looks like this:

Ovservable<List<Clazz>> getClasses(@Header("Authorization") String auth, @Path("user_id")int user_id);

I call it from my fragment like this:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_classes, container, false);
Retrofit retrofit = new Retrofit.Builder()

RecyclerView classesRV = (RecyclerView)rootView.findViewById(;
ClassInterface classAPI = retrofit.create(ClassInterface.class);
Observable<List<Class>> classes = classAPI.getClasses("Basic " +Base64.encodeToString("".getBytes(), Base64.NO_WRAP), 1);

.subscribe(new Subscriber<List<Class>>() {
public void onCompleted() {


public void onError(Throwable e) {

public void onNext(List<Class> classes) {
classesRV.setAdapter(new ClassRecyclerViewAdapter(classes, getContext()));
classesRV.setLayoutManager(new LinearLayoutManager(getContext()));
return rootView;

My Class object looks like this along with getters and setters:

public class Class {
private int id;
private String name;
private int badge;
private String professor;
private String location;
private String days;
private String time;
private int user_id;
private List<Assignment> assignments;

public Class() {

Shouldn't gson see the "classes": [] array and parse the objects from it? What have I done wrong

Answer Source

As @CommonsWare has mentioned, with this JSON response structure you would need some intermediate class to hold the collection of Clazzs (or write a custom deserializer altogether). Like this:

public class ClassHolder {

    List<Clazz> classes;


The return type of getClasses() would change to Observable<ClassHolder> accordingly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download