-2

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.

halfer
  • 18,701
  • 13
  • 79
  • 158
FreddCha
  • 269
  • 1
  • 4
  • 12
  • What is the problem you are having? –  Sep 15 '17 at 21:16
  • can you more clearly state the actual question? – lloyd Sep 15 '17 at 21:20
  • Also, note that recent versions of Retrofit make it very easy to log the raw HTTP conversation. –  Sep 15 '17 at 21:20
  • I am getting this error **Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $** – FreddCha Sep 15 '17 at 21:21
  • Please update the question with these details (Also, **where** are you seeing this error, and **when**?). Anyway, you are giving it an object, not an array. See: https://stackoverflow.com/q/36177629/1531971 –  Sep 15 '17 at 21:22
  • But from the sample data I provided, the API is returning an array of objects so I cant seem to understand this error, please help and be patient I am a bit new to programming – FreddCha Sep 15 '17 at 21:32
  • You should try and understand the dup I found. It is almost certainly the same problem. –  Sep 15 '17 at 23:28
  • *cant seem to find a solution. I have Googled around but to no avail* what exactly are you googling? There are a million questions about this already, I don't believe you didn't find any of them – Tim Sep 18 '17 at 09:42

1 Answers1

0

Yes, the API is not returning one message, it is returning an array of messages. So you need to map it as a list, not as a single message.

So I suggest that: 1. You create an additional model, bean or pojo. You can call it MessageList, and it would look something like:

public class MessagesList {
  private List<Message> messageList;
//add getters and setters .. etc.
}
  1. I think you should also change List<Message> in your code to MessageList instead.

This should allow the list of messages returned by the API to be mapped to the MessageList model, each individual message will be mapped to a Message object with it's (unique?) id, title and details. And you are able now to iterate through the list to get each Message.

Good luck

ninjayoto
  • 623
  • 1
  • 6
  • 14
  • Thank you for you suggestion. But I realized that the code is ok as it is, only that I was using a wrong API url. – FreddCha Sep 19 '17 at 11:59