-1

I am trying to parse JSON code like this below :

[
{
    "id": 1,
    "name": "Кафе 1",
    "tracks": [
           I think problem is here 
        {
            "id": 10,
            "name": "track 2.mp3",
            "url": "track 2.mp3",
       ...

This is my ApiInterface.java

@GET(".../playlists")
Call<FDYPlaylists> getPlaylists(@HeaderMap Map<String, String> headers);

ApiUtils.java

public static final String BASE_URL = "url";

public static APIService getAPIService() {

    return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}

You can find below my RetrofitClient.java

 private static Retrofit retrofit = null;

public static Retrofit getClient(String baseUrl) {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

And the call is like below :

 Map<String, String> map = new HashMap<>();
    map.put("Content-Type", "application/json");
    map.put("Authorization", "Bearer " + token);

    mAPIService.getPlaylists(map).enqueue(new Callback<FDYPlaylists>() {
       .....

This is the error I am getting:

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2

Shashanth
  • 4,995
  • 7
  • 41
  • 51

2 Answers2

2

The Square brackets reflects that there is array of objects, so please use the below code

@GET(".../playlists")
Call<ArrayList<FDYPlaylists>> getPlaylists(@HeaderMap Map<String, String> headers);
Awais Tariq
  • 334
  • 2
  • 9
0

The exception tells you this in that you are expecting an object at the root but the real data is actually an array. This means you need to change the type to be an array.

Arty
  • 859
  • 2
  • 12
  • 31