【问题标题】:Can not deserialize instance of java.lang.Boolean out of START_ARRAY token无法从 START_ARRAY 令牌中反序列化 java.lang.Boolean 的实例
【发布时间】:2019-10-25 04:35:28
【问题描述】:

我需要处理调用的返回,但是它可以返回布尔值或对象。找到电话号码后,它会直接崩溃到 onFailure。

我正在寻找解决方案,但所有发现都没有解决我的问题。

我的界面:

public interface PessoaService {

    @FormUrlEncoded
    @POST("Pessoa/BuscaContatosAgenda/")
    Call<Boolean> buscaContatoAgenda(@Field("Identificador") String identificador,
                                     @Field("UnidadeId") String unidadeId,
                                     @Field("Telefones") ArrayList<String> telefone);
}

我的改造配置

public class RetrofitConfig {
    private final Retrofit retrofit;

    public RetrofitConfig() {
        this.retrofit = new Retrofit.Builder()
                .baseUrl(DadosEmpresa.URL)
                //O método addConverterFactory recebe a classe que será responsável por lidar com a conversão
                .addConverterFactory(JacksonConverterFactory.create())
                //Utilizamos o método build para criar o objeto Retrofit
                .build();
    }

    public PessoaService getBuscaContatoAgenda() {
        return this.retrofit.create(PessoaService.class);
    }
}

我的活动功能:

 private void verificandoContatos() {
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null, null, ContactsContract.Contacts.DISPLAY_NAME);

        telefonesArrayList = new ArrayList<>();
        while (cursor.moveToNext()) {
            String telefone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            telefonesArrayList.add(telefone);
        }
        cursor.close();

        Call<Boolean> call = new RetrofitConfig().getBuscaContatoAgenda().buscaContatoAgenda(pessoa.getIdentificador(), DadosEmpresa.UnidadeID, telefonesArrayList);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                if (response.body() == true) {
                    Toast.makeText(AgendaContatoActivity.this, "Nenhum contato foi encontrado na sua agenda!", Toast.LENGTH_LONG).show();
                } else if (response.body() == false) {
                    Toast.makeText(AgendaContatoActivity.this, "Ocorreu um erro inesperado. Tente novamente mais tarde.", Toast.LENGTH_LONG).show();
                    Log.e(TAG, "onResponse: " + response.body().toString());
                } else {
                    Toast.makeText(AgendaContatoActivity.this, "Esses são seus contatos encontrados", Toast.LENGTH_LONG).show();
                    createRecyclerView();
                }
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Log.e(TAG, "onFailure: Erro ao enviar contatos: " + t.getMessage());
                Toast.makeText(getBaseContext(), "Ocorreu um erro inesperado. Tente novamente.", Toast.LENGTH_LONG).show();

            }
        });
    }

t.getMessage = com.fasterxml.jackson.databind.JsonMappingException: 无法从 START_ARRAY 反序列化 java.lang.Boolean 的实例 [来源:okhttp3.ResponseBody$BomAwareReader@f41ca80;线: 1、栏目:1]

编辑/更新/解决方案

在 cmets 的帮助下,我找到了解决问题的方法。不是最好的方法,但适用于这种情况:

 private void verificandoContatos() {
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null, null, null, ContactsContract.Contacts.DISPLAY_NAME);

        telefonesArrayList = new ArrayList<>();
        while (cursor.moveToNext()) {
            String telefone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            telefonesArrayList.add(telefone);
        }
        cursor.close();

        Call<ArrayList<GetContatoAgenda>> call = new RetrofitConfig().getBuscaContatoAgenda().buscaContatoAgenda(pessoa.getIdentificador(), DadosEmpresa.UnidadeID, telefonesArrayList);
        call.enqueue(new Callback<ArrayList<GetContatoAgenda>>() {
            @Override
            public void onResponse(Call<ArrayList<GetContatoAgenda>> call, Response<ArrayList<GetContatoAgenda>> response) {
                Toast.makeText(AgendaContatoActivity.this, "Esses são seus contatos encontrados em nossa aplicativo!", Toast.LENGTH_LONG).show();
                contatoArrayList = new ArrayList<GetContatoAgenda>();
                //Como a Callback do Retrofit já faz o mapeamento, então fazemos o contatoArrayList receber a response.body();
                contatoArrayList = response.body();
                createRecyclerView();
            }

            @Override
            public void onFailure(Call<ArrayList<GetContatoAgenda>> call, Throwable t) {
                Log.e(TAG, "onFailure: Erro ao enviar contatos: " + t.getMessage());
                Toast.makeText(getBaseContext(), "Ocorreu um erro inesperado. Tente novamente.", Toast.LENGTH_LONG).show();

            }
        });
    }

【问题讨论】:

    标签: android serialization jackson


    【解决方案1】:

    问题出在您的模型中。您需要确保您获得的 JSON 响应 100% 适合您用于反序列化的模型 这条线有问题。这里假设是您在 cmets 中显示的模型

    Call<Boolean> call
    

    当您收到 JSON 响应时,您应该从中解析数据。这是一个很好的话题如何做到这一点 Android parse JSONObject

    【讨论】:

    • 这是我的问题。我收到 2 个 JSON 响应。有没有办法进行通用改造回调?
    • @GabrielJúniordeSouza 您必须为每种类型的响应制作模型并将响应正确连接到模型。它不会为您生成模型。很高兴为您提供帮助
    • @GabrielJúniordeSouza 显示你的回复 你得到了什么?我猜它应该是一个不是布尔值的对象。
    • 响应返回多个结果。如果TRUE,返回一个消息(因为没有找到联系人),如果FALSE,返回一个错误消息,如果捕获另一个异常,返回一个ArrayList联系方式
    • @GabrielJúniordeSouza TRUE 或 FALSE 是结果的值。响应假设是一个对象,其中值可以是真或假。这个响应来自哪里?给个全图
    猜你喜欢
    • 2020-09-23
    • 2019-12-22
    • 1970-01-01
    • 2016-07-30
    • 2015-03-10
    • 2015-12-24
    • 2018-12-03
    • 2020-07-07
    • 2011-12-02
    相关资源
    最近更新 更多