Android + Retrofit 2 + GSON =无法为接口调用no-args构造函数

时间:2017-07-15 05:17:41

标签: java android retrofit retrofit2 gson

我试图在我的应用中使用改装2,并且我得到了以下错误:

java.lang.RuntimeException: Unable to invoke no-args constructor for interface box.gov.br.ourapp.API.ClientePFApi. Register an InstanceCreator with Gson for this type may fix this problem.

而且......我不知道问题在哪里= /

我的java课程:

public class ClientePFVisao360 {

private static final long serialVersionUID = 3182722297876508581L;

@SerializedName("txCPF")
@Expose
public String txCPF;

@SerializedName("nuCocli")
@Expose
public Long nuCocli;

@SerializedName("txNomeCliente")
@Expose
public String txNomeCliente;

@SerializedName("dtNascimento")
@Expose
public String dtNascimento;

@SerializedName("dtInicioRelacionamento")
@Expose
public String dtInicioRelacionamento;

@SerializedName("txSegmento")
@Expose
public String txSegmento;

@SerializedName("txOcupacao")
@Expose
public String txOcupacao;

@SerializedName("txSexo")
@Expose
public String txSexo;

@SerializedName("txEstadoCivil")
@Expose
public String txEstadoCivil;

@SerializedName("txNivelInstrucao")
@Expose
public String txNivelInstrucao;

@SerializedName("txTipoPessoa")
@Expose
public String txTipoPessoa;

@SerializedName("nuNacionalidade")
@Expose
public Integer nuNacionalidade;

@SerializedName("txNaturalidade")
@Expose
public String txNaturalidade;

@SerializedName("txNomePai")
@Expose
public String txNomePai;

@SerializedName("txNomeMae")
@Expose
public String txNomeMae;

@SerializedName("txDeficiencia")
@Expose
public String txDeficiencia;

@SerializedName("nichos")
@Expose
public List<String> nichos;

@SerializedName("conjuge")
@Expose
public Conjuge conjuge;

@SerializedName("renda")
@Expose
public Renda renda;

@SerializedName("meiosComunicacao")
@Expose
public MeioComunicacao meiosComunicacao;

@SerializedName("carteiraGRC")
@Expose
public List<CarteiraGrc> carteiraGRC;

//getters and setters....

public ClientePFVisao360() {
}

我的界面:

public interface ClientePFApi {
    @GET("clientepf/{user}")
    Call<ClientePFApi> getClientePF(@Path("user") String user);
}

我怎么称呼这个:

    OkHttpClient okClient = new OkHttpClient();
    Retrofit client = new Retrofit.Builder()
            .baseUrl(API)
            .client(okClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    ClientePFApi service = client.create(ClientePFApi.class);
    Call<ClientePFApi> call = service.getClientePF("bigua");

    call.enqueue(new Callback<ClientePFApi>() {
        @Override
        public void onResponse(@NonNull Call<ClientePFApi> call, @NonNull Response<ClientePFApi> response) {
            Log.e("retrofit", "ok");

        }

        @Override
        public void onFailure(@NonNull Call<ClientePFApi> call, @NonNull Throwable t) {
            Log.e("trow", t.toString());
            Log.e("retrofit", "crash");
        }
    });

我从我的API收到的JSON:

  

{&#34; txCPF&#34;:&#34; 1234567&#34;&#34; nuCocli&#34;:12345&#34; txNomeCliente&#34;:&#34; BLA&#34; &#34; dtNascimento&#34;:&#34; 12 /一千九百八十四分之一十一&#34;&#34; dtInicioRelacionamento&#34;:&#34; 04/05/2010&#34;&#34; txSegmento& #34;:&#34; BLA&#34;&#34; txOcupacao&#34;:&#34; BLA&#34;&#34; txSexo&#34;:&#34; BLA&#34;,& #34; txEstadoCivil&#34;:&#34; BLA&#34;&#34; txNivelInstrucao&#34;:&#34; BLA&#34;&#34; txTipoPessoa&#34;:&#34; BLA&# 34;,&#34; nuNacionalidade&#34;:空,&#34; txNaturalidade&#34;:&#34; DF&#34;&#34; txNomePai&#34;:&#34;等等&#34 ;, &#34; txNomeMae&#34;:&#34; BLA&#34;&#34; txDeficiencia&#34;:空,&#34; nichos&#34;:空,&#34; conjuge&#34;:空&#34;仁达&#34;:空,&#34; meiosComunicacao&#34;:空,&#34; carteiraGRC&#34;:空}

我很抱歉,如果它是一个菜鸟问题/问题,但我在这里读了很多问题而且没有弄到我的错误。

感谢任何帮助的建议。

1 个答案:

答案 0 :(得分:3)

我认为存在问题,因为您为interface课程和Model课程使用相同的名称。

这两个名称都是ClientePFApi

public interface ClientePFApi {
    @GET("clientepf/{user}")
    Call<ClientePFApi> getClientePF(@Path("user") String user);
}

您使用ClientePFVisao360,如下所示

public interface ClientePFApi {
        @GET("clientepf/{user}")
        Call<ClientePFVisao360> getClientePF(@Path("user") String user);
    }