I need to get a list of orders through Rest API. The JSON from the server looks as follows:
{
"meta":
{
"status": "SUCCESS",
"message": "Data was fetched successfully."
},
"data":
{
"orders":
[
{
"id": "1",
"seller": "some value"
},
{
"id": "2",
"seller": "some value"
}
]
}
}
I use ORMLite
, and the model looks like this:
@DatabaseTable(tableName = Order.TABLE_NAME)
public class Order {
public static final String TABLE_NAME = "Order";
@DatabaseField
private String id;
@DatabaseField
private String seler;
public Order() {
}
// getters and setters
}
Retrofit 2
is used to perform queries:
@GET("/api/orders/get")
Call<RestResponse<List<Order>>> getOrdersList();
And, finally, RestResponse
class is as follows:
public class RestResponse<T> {
private Meta meta;
private T data;
public RestResponse() {
meta = new Meta();
}
public Meta getMeta() {
return meta;
}
public T getData() {
return data;
}
public static class Meta {
private Status status;
private String message;
// getters and setters
}
}
When performing this query, I get the following error:
Expected BEGIN_ARRAY but was BEGIN_OBJECT
The error's reason is clear: callback intends to obtain the object, but gets the array. What can be done in this situation if I want to get an array of elements with <Order>
type?
Thanks!
Field data
public class RestResponse<T> {
private Meta meta;
private T data;
...
}
Must be object, not list.
Add Orders
class:
public class Orders {
List<Order> orders;
...
}
And modify retrofit call:
Call<RestResponse<Orders>> getOrdersList();