I have this problem and cant seem to find a solution. I have Googled around but to no avail.
The API is returning an array of objects but gson has found an object.
My Model
public class Message {
private int id;
private String title;
private String details; }
MessageClient
public interface MessageClient {
@GET("/api/test")
Call<List<Message>> getMessages();}
Implementation
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
MessageClient client = retrofit.create(MessageClient.class);
Call<List<Message>> call = client.getMessages();
call.enqueue(new Callback<List<Message>>() {
@Override
public void onResponse(Call<List<Message>> call, Response<List<Message>> response) {
List<Message> messageList = response.body();
Log.d(TAG, "LIST: "+messageList);
}
@Override
public void onFailure(Call<List<Message>> call, Throwable t) {
Log.d(TAG, "API ERROR: "+t.getMessage());
}
});
Sample data from API
[
{
"id": 1,
"title": "Title 1",
"details": "Message1.",
"client_id": 1,
},
{
"id": 2,
"title": "Title 2",
"details": "Message 2.",
"client_id": 1,
}]
Where am I missing it? Please note the API sample data.