RETROFIT2 无法将 START_OBJECT 令牌反序列化为 java.util.ArrayList 实例

7 浏览
0 Comments

RETROFIT2 无法将 START_OBJECT 令牌反序列化为 java.util.ArrayList 实例

我在我的通话中遇到了一些问题。我的回调函数总是返回onFailure,并显示以下错误:

onResponse: com.fasterxml.jackson.databind.JsonMappingException: 无法从START_OBJECT令牌中反序列化java.util.ArrayList的实例

at [Source: okhttp3.ResponseBody$BomAwareReader@4c32516; line: 1, column: 1]

API响应farms/abstract(列表中有一项):

[

{

"id": "0fb30c45-00d9-4171-9673-85991dca5db2",

"name": "Areado"

}

]

这是我的接口:

public interface FarmService {
    @GET("farms/abstract")
    Call> getFarmList(@Header("Authorization") String token);
}

这是我的方法/函数:

private void callGetFarmList() {
        try {
            Call> call = new RetrofitConfig().getFarmList().getFarmList(authToken);
            call.enqueue(new Callback>() {
                @Override
                public void onResponse(@NonNull Call> call, @NonNull Response> response) {
                    if (response.isSuccessful()) {
                        if (response.body() != null) {
                            int codeResult = response.code();
                            switch (codeResult) {
                                case 200:
                                    farmListArrayList = new ArrayList();
                                    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> 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);
        }
    }

更新

这是我的类:

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 + '\'' +
                '}';
    }
}

更新 2020年5月25日

我改为使用GsonConverterFactory而不是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);
    }
}

最后的解决方案

后端给我发送了一个新的响应,现在它像魅力一样工作。但还是感谢@KasımÖzdemir花时间帮助我

{

"farms": [

{

"id": "0fc2b30c-4500-c399-4171-e2809373e280",

"name": "Areado"

},

{

"id": "81041a28-1193-4310-b616-fabcabdf63d7",

"name": "Fazenda 3"

}

]

}

0