Search code examples
javaandroidretrofitretrofit2gson

Android + Retrofit 2 + GSON = Unable to invoke no-args constructor for interface


I'm trying to use retrofit 2 in my app and i'm getting the folloing error:

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.

And... I dunno where's the problem =/

My java class:

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() {
}

My Interface:

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

How I'm calling this:

    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");
        }
    });

The JSON i'm received from my API:

{"txCPF":"1234567","nuCocli":12345,"txNomeCliente":"bla","dtNascimento":"12/11/1984","dtInicioRelacionamento":"04/05/2010","txSegmento":"bla","txOcupacao":"bla","txSexo":"bla","txEstadoCivil":"bla","txNivelInstrucao":"bla","txTipoPessoa":"bla","nuNacionalidade":null,"txNaturalidade":"DF","txNomePai":"blah","txNomeMae":"bla","txDeficiencia":null,"nichos":null,"conjuge":null,"renda":null,"meiosComunicacao":null,"carteiraGRC":null}

I'm sorry if its a noob problem/question, but i've read many questions here and don't get where is my mistake.

thanks in advice for any help.


Solution

  • I think there is a problem because you user same name for interface class and your Model class.

    both are same name here ClientePFApi .

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

    you use ClientePFVisao360 like below

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