0

I have some troubles with my call. My callback always return onFailure with this error:

onResponse: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: okhttp3.ResponseBody$BomAwareReader@4c32516; line: 1, column: 1]

The API Response farms/abstract (with one item on list):

[
    {
        "id": "0fb30c45-00d9-4171-9673-85991dca5db2",
        "name": "Areado"
    }
]

This is my Interface:

public interface FarmService {

    @GET("farms/abstract")
    Call<ArrayList<FarmList>> getFarmList(@Header("Authorization") String token);
}

And this is my method/function:

private void callGetFarmList() {
        try {
            Call<ArrayList<FarmList>> call = new RetrofitConfig().getFarmList().getFarmList(authToken);
            call.enqueue(new Callback<ArrayList<FarmList>>() {
                @Override
                public void onResponse(@NonNull Call<ArrayList<FarmList>> call, @NonNull Response<ArrayList<FarmList>> response) {
                    if (response.isSuccessful()) {
                        if (response.body() != null) {

                            int codeResult = response.code();

                            switch (codeResult) {
                                case 200:
                                    farmListArrayList = new ArrayList<FarmList>();
                                    farmListArrayList = response.body();
                                    createFarmRecyclerView();
                                    break;
                                case 201:
                                    /*Created*/
                                    Log.d("201", "onResponse: 201");
                                    break;
                                case 401:
                                    /*Unauthorized*/

                                    break;
                                case 403:
                                    /*Forbidden*/

                                    break;
                                case 404:
                                    /*Error*/

                                    break;
                                default:
                                    break;
                            }
                        }
                    } else {
                        //Error
                    }
                }

                @Override
                public void onFailure(@NonNull Call<ArrayList<FarmList>> call, @NonNull Throwable t) {
                    Toast.makeText(FazendaActivity.this, "OK", Toast.LENGTH_SHORT).show();
                    Log.d("getFarmList", "onResponse: " + t);
                }
            });
        } catch (Exception e) {
            Log.d("erroGetFarmList", "callGetFarmList: " + e);
        }
    }

UPDATED

This is my class:

public class FarmList {
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("name")
    @Expose
    private String name;

    public FarmList() {
        super();
    }

    public FarmList(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "FarmList{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

UPDATED 25/05/2020

I changed to GsonConverterFactory instead JacksonConverterFactory

public class RetrofitConfig {
    private final Retrofit retrofit;

    public RetrofitConfig() {
        this.retrofit = new Retrofit.Builder()
                .baseUrl("MY_BASE_URL")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public LoginService getLogin() {
        return this.retrofit.create(LoginService.class);
    }

    public FarmService getFarm() {
        return this.retrofit.create(FarmService.class);
    }

    public FarmService getFarmList() {
        return this.retrofit.create(FarmService.class);
    }
}

FINAL SOLUTION The BE send me a new response and now it's working like a charm. But thanks @KasımÖzdemir, for the time to help me

{
    "farms": [
        {
            "id": "0fc2b30c-4500-c399-4171-e2809373e280",
            "name": "Areado"
        },
        {
            "id": "81041a28-1193-4310-b616-fabcabdf63d7",
            "name": "Fazenda 3"
        }
    ]
}

0 Answers0