Retrofit返回200 OK和正确的JSON,但parcelable为null

时间:2018-02-16 14:56:29

标签: android json retrofit2 parcelable okhttp3

我正在拨打以下电话:

@GET("request/view/{requestId}.json")
Call<Solicitacao> visualizar(@Path("requestId") Integer requestId);

返回200 OK和预期的JSON:

{
    "id": 12227,
    "responsaveis_ID": 2657,
    "horario": "00:00",
    "status": "solicitado",
    "preco_boleto": null,
    "preco_cartao": null,
    "vans_ID": 1103,
    "lat": -25.4361961,
    "lng": -49.2683197,
    "escolas_ID": 118645,
    "assentos": 1,
    "forma_pagamento": null,
    "parcelas": null,
    "data_primeira_parcela": null,
    "created": "2018-02-16T10:41:02",
    "reason": null,
    "modified": "2018-02-16T10:41:02",
    "data_melhor_parcela": null,
    "referencia": "...",
    "horario_volta": null,
    "trecho": "ida",
    "turno": "manha",
    "data_inicio": null,
    "data_fim": null,
    "contra_proposta": 0,
    "contra_boleto": null,
    "contra_cartao": null,
    "ano_letivo": 2018,
    "logradouro": "...",
    "numero": " ...",
    "bairro": " ...",
    "cidade": " ...",
    "van": {
        "ID": 1103,
        "IDCLIENTE": ...,
        "VEICULO": "...",
        "CAPACIDADE": 20,
        "OCUPADOS": null,
        "DDD1": "41",
        "TELEFONE1": "...9",
        "DDD2": "41",
        "TELEFONE2": "...",
        "DDD3": "41",
        "TELEFONE3": "...",
        "DDD4": "41",
        "TELEFONE4": "...",
        "PLACA": "...",
        "RENAVAN": "",
        "ANO": "2016",
        "MODELO": "CLIO",
        "FABRICANTE": "RENAULT",
        "STP_GETAX": "111X",
        "ENDERECO_GARAGEM": "...",
        "url_certificate": null,
        "expiration_date": null,
        "created": "2017-10-09T14:32:18",
        "modified": "2018-01-26T16:32:12",
        "route_execucao": 327,
        "wifi": 0,
        "tv": 0,
        "ar_condicionado": 0,
        "habilitation_file": null,
        "stp_getax_file": null,
        "clientes": {
            "ID": ...,
            "IDUSUARIO": ...,
            "NOME": "...",
            "CPF": "...",
            "ENDERECO": "...",
            "NUMERO": "...",
            "CEP": "...",
            "MUNICIPIO": "...",
            "UF": "...",
            "RG": "",
            "DATACOB": 12,
            "IDVEND": 12,
            "EMAIL": null,
            "VALOR_MENSAL": 0,
            "neighborhood_id": null,
            "DDD": null,
            "FONE": null,
            "COMPLEMENTO": null,
            "token_boleto": "...",
            "primeiro_resgate": 10,
            "segundo_resgate": 20,
            "usuarios": {
                "ID": ...,
                "NOME": "...",
                "USUARIO": "...",
                "SENHA": "...",
                "EMAIL": "...",
                "NIVEL": 1,
                "IDCEL": "...",
                "IDVEND": 12,
                "SALT": null,
                "HASH": "...",
                "status": 1,
                "tipo": "condutor",
                "contract_accepted": "2018-02-06T00:00:00"
            }
        }
    },
    "responsavel": {
        "ID": 2657,
        "NOME": "...",
        "EMAIL": "...",
        "DDD": null,
        "FONE": null,
        "DDDCEL": "...",
        "CELULAR": "...",
        "IDALUNO": null,
        "ENDERECO": null,
        "CPF": null,
        "NUMERO": null,
        "BAIRRO": null,
        "CIDADE": null,
        "UF": null,
        "IDCELULAR": "...",
        "DDDCEL2": null,
        "DDDCEL3": null,
        "NUMCEL2": null,
        "NUMCEL3": null,
        "IDCELULAR2": null,
        "IDCELULAR3": null,
        "IDVAN": null,
        "status": "inativo",
        "created": "2018-01-23T14:06:47",
        "modified": "2018-02-15T09:58:23",
        "usuarios_ID": 5696
    },
    "school": {
        "ID": 118645,
        "NOME": "...",
        "ENDERECO": "...",
        "NUMERO": ...,
        "BAIRRO": "...",
        "DDD": "...",
        "FONE": "...",
        "UF": "PR",
        "CIDADE": "...",
        "STATUS": "IMPORTADA",
        "lat": -25.42395,
        "lng": -49.250295,
        "Endereco_formatado": null,
        "leader_id": null
    }
}

我正在使用以下模型:

public class Solicitacao implements Parcelable {
    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("forma_pagamento")
    @Expose
    private String formaPagamento;
    @SerializedName("parcelas")
    @Expose
    private Integer parcelas;
    @SerializedName("data_primeira_parcela")
    @Expose
    private String dataPrimeiraParcela;
    @SerializedName("referencia")
    @Expose
    private String referencia;
    @SerializedName("horario_volta")
    @Expose
    private String horarioVolta;
    @SerializedName("data_inicio")
    @Expose
    private String dataInicio;
    @SerializedName("data_fim")
    @Expose
    private String dataFim;
    @SerializedName("contra_proposta")
    @Expose
    private Integer contraProposta;
    @SerializedName("responsaveis_ID")
    @Expose
    private Integer responsaveisID;
    @SerializedName("horario")
    @Expose
    private String horario;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("preco_boleto")
    @Expose
    private Double precoBoleto;
    @SerializedName("preco_cartao")
    @Expose
    private Double precoCartao;
    @SerializedName("contra_cartao")
    @Expose
    private Double contraCartao;
    @SerializedName("contra_boleto")
    @Expose
    private Double contraBoleto;
    @SerializedName("vans_ID")
    @Expose
    private Integer vansID;
    @SerializedName("lat")
    @Expose
    private Double lat;
    @SerializedName("lng")
    @Expose
    private Double lng;
    @SerializedName("escolas_ID")
    @Expose
    private Integer escolasID;
    @SerializedName("assentos")
    @Expose
    private Integer assentos;
    @SerializedName("responsavel")
    @Expose
    private Responsavel responsavel;
    @SerializedName("school")
    @Expose
    private School school;
    @SerializedName("van")
    @Expose
    private Van van;
    @SerializedName("turno")
    @Expose
    private String turno;
    @SerializedName("trecho")
    @Expose
    private String trecho;
    @SerializedName("logradouro")
    @Expose
    private String logradouro;
    @SerializedName("numero")
    @Expose
    private String numero;
    @SerializedName("bairro")
    @Expose
    private String bairro;
    @SerializedName("cidade")
    @Expose
    private String cidade;

    public Solicitacao() {

    }

    //getters and setters...

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(this.id);
        dest.writeString(this.formaPagamento);
        dest.writeValue(this.parcelas);
        dest.writeString(this.dataPrimeiraParcela);
        dest.writeString(this.referencia);
        dest.writeString(this.horarioVolta);
        dest.writeString(this.dataInicio);
        dest.writeString(this.dataFim);
        dest.writeValue(this.contraProposta);
        dest.writeValue(this.responsaveisID);
        dest.writeString(this.horario);
        dest.writeString(this.status);
        dest.writeValue(this.precoBoleto);
        dest.writeValue(this.precoCartao);
        dest.writeValue(this.contraCartao);
        dest.writeValue(this.contraBoleto);
        dest.writeValue(this.vansID);
        dest.writeValue(this.lat);
        dest.writeValue(this.lng);
        dest.writeValue(this.escolasID);
        dest.writeValue(this.assentos);
        dest.writeParcelable(this.responsavel, flags);
        dest.writeParcelable(this.school, flags);
        dest.writeParcelable(this.van, flags);
        dest.writeString(this.turno);
        dest.writeString(this.trecho);
        dest.writeString(this.logradouro);
        dest.writeString(this.numero);
        dest.writeString(this.bairro);
        dest.writeString(this.cidade);
    }

    protected Solicitacao(Parcel in) {
        this.id = (Integer) in.readValue(Integer.class.getClassLoader());
        this.formaPagamento = in.readString();
        this.parcelas = (Integer) in.readValue(Integer.class.getClassLoader());
        this.dataPrimeiraParcela = in.readString();
        this.referencia = in.readString();
        this.horarioVolta = in.readString();
        this.dataInicio = in.readString();
        this.dataFim = in.readString();
        this.contraProposta = (Integer) in.readValue(Integer.class.getClassLoader());
        this.responsaveisID = (Integer) in.readValue(Integer.class.getClassLoader());
        this.horario = in.readString();
        this.status = in.readString();
        this.precoBoleto = (Double) in.readValue(Double.class.getClassLoader());
        this.precoCartao = (Double) in.readValue(Double.class.getClassLoader());
        this.contraCartao = (Double) in.readValue(Double.class.getClassLoader());
        this.contraBoleto = (Double) in.readValue(Double.class.getClassLoader());
        this.vansID = (Integer) in.readValue(Integer.class.getClassLoader());
        this.lat = (Double) in.readValue(Double.class.getClassLoader());
        this.lng = (Double) in.readValue(Double.class.getClassLoader());
        this.escolasID = (Integer) in.readValue(Integer.class.getClassLoader());
        this.assentos = (Integer) in.readValue(Integer.class.getClassLoader());
        this.responsavel = in.readParcelable(Responsavel.class.getClassLoader());
        this.school = in.readParcelable(School.class.getClassLoader());
        this.van = in.readParcelable(Van.class.getClassLoader());
        this.turno = in.readString();
        this.trecho = in.readString();
        this.logradouro = in.readString();
        this.numero = in.readString();
        this.bairro = in.readString();
        this.cidade = in.readString();
    }

    public static final Creator<Solicitacao> CREATOR = new Creator<Solicitacao>() {
        @Override
        public Solicitacao createFromParcel(Parcel source) {
            return new Solicitacao(source);
        }

        @Override
        public Solicitacao[] newArray(int size) {
            return new Solicitacao[size];
        }
    };
}

但我的parcelable(Solicitacao)对象在设置响应后变为null:

solicitacaoManager.visualizar(solicitacaoPushId).subscribe(solicitacao -> {
    view.hideLoading();

    //Setting the object:
    this.solicitacao = solicitacao;

}, throwable -> {
    throwable.printStackTrace();
    getActivity().finish();
});

我收到以下错误:

Cannot read raw response body of a converted body.

创建Retrofit实例的代码:

private T buildService(Class<T> serviceClass) {
    OkHttpClient.Builder build = new OkHttpClient.Builder();
    build.connectTimeout(60, TimeUnit.SECONDS);
    build.readTimeout(60, TimeUnit.SECONDS);

    if(getHttpInterceptors() != null){
        for(Interceptor interceptor : getHttpInterceptors()){
            build.addInterceptor(interceptor);
        }
    }
    OkHttpClient okHttpClient = build.build();

    Retrofit.Builder restBuilder = new Retrofit.Builder()
            .baseUrl(getBaseUrl())
            .addConverterFactory(GsonConverterFactory.create(getGson()))
            .client(okHttpClient);

    Retrofit restAdapter = restBuilder.build();
    return restAdapter.create(serviceClass);
}

我在这里检查了很多相关问题,但没有一个能帮我解决问题。有人知道如何解决这个问题吗?非常感谢。

我正在使用Retrofit 2.0.0和OkHttp 3.2.0。

0 个答案:

没有答案