0

I have the below JSON structure:

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  }
]

I am trying to consume this JSON using Retrofit 2.x .I am trying to get the list of users.

Here is my APIService interface:

public interface APIService {

    @POST("posts")
    Call<List<FakeJSON>> getItems();
}

Here is my POJO class:

public class FakeJSON {

    @SerializedName("userId")
    @Expose
    private int userId;
    @SerializedName("id")
    @Expose
    private int id;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("body")
    @Expose
    private String body;

    /**
     * No args constructor for use in serialization
     *
     */
    public FakeJSON() {
    }

    /**
     *
     * @param id
     * @param body
     * @param title
     * @param userId
     */
    public FakeJSON(int userId, int id, String title, String body) {
        super();
        this.userId = userId;
        this.id = id;
        this.title = title;
        this.body = body;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

}

Finally here is my Activity code where I am making the network call:

public class NetworkActivity extends AppCompatActivity {

    private static Retrofit.Builder builder = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_network);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        builder = new Retrofit.Builder()
                .baseUrl("https://jsonplaceholder.typicode.com/")
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit =
                builder
                        .client(
                                httpClient.build()
                        )
                        .build();
        APIService apiService = retrofit.create(APIService.class);

        Call<List<FakeJSON>> listCall = apiService.getItems();

        listCall.enqueue(new Callback<List<FakeJSON>>() {
            @Override
            public void onResponse(Call<List<FakeJSON>> call, Response<List<FakeJSON>> response) {
                try {
                    Log.e("List Size", response.body().size()+"");
                }catch (NullPointerException e){
                    e.printStackTrace();;
                }
            }

            @Override
            public void onFailure(Call<List<FakeJSON>> call, Throwable t) {
                Log.e("Error", t.getMessage());
            }
        });
    }
}

Whenever I am running the application I am getting the following exception:

E/Error: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

What am I doing wrong here?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sagar suri
  • 4,351
  • 12
  • 59
  • 122

1 Answers1

1

'E/Error: Expected BEGIN_ARRAY but was BEGIN_OBJECT

This means that a retrofit is facing JSON Object instead of an array. Maybe you're getting some error from the server? Try to manually do the same query and see the response. Maybe this way you'll figure out if something is wrong with the request done from the code.

ApriOri
  • 2,618
  • 29
  • 48