Android + Retrofit 2 + GSON =无法调用接口的无参数构造函数

smdncfj3  于 2022-11-06  发布在  Android
关注(0)|答案(3)|浏览(210)

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.

cfh9epnr

cfh9epnr1#

我认为有一个问题,因为您使用相同的名称为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);
    }
9lowa7mx

9lowa7mx2#

例如,如果您API服务方法是

interface ExampleInterface {
     @POST("sample")
     suspend  fun sampleFun(): Call<JsonObject>
}

将其更改为

interface ExampleInterface {
     @POST("sample")
     fun sampleFun(): Call<JsonObject>
}
z0qdvdin

z0qdvdin3#

您必须将<Call>更改为<Response>

相关问题