Plays2 Plays2 - 2 months ago 17
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:

@GET("api/users/{user_id}/classes")
Ovservable<List<Clazz>> getClasses(@Header("Authorization") String auth, @Path("user_id")int user_id);


I call it from my fragment like this:

@Override
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()
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(Sched.API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

RecyclerView classesRV = (RecyclerView)rootView.findViewById(R.id.classRec);
ClassInterface classAPI = retrofit.create(ClassInterface.class);
Observable<List<Class>> classes = classAPI.getClasses("Basic " +Base64.encodeToString("joyod3@gmail.com:nightman_420".getBytes(), Base64.NO_WRAP), 1);

classes.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<Class>>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {
e.printStackTrace();
}

@Override
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

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.

Comments