Android + Retrofit 2 + GSON =无法为接口调用无参数构造函数 [英] Android + Retrofit 2 + GSON = Unable to invoke no-args constructor for interface

查看:433
本文介绍了Android + Retrofit 2 + GSON =无法为接口调用无参数构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在应用中使用翻新2,但出现以下错误:

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 =/

我的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:

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," nicos:null," conjuge:null," renda: null,"meiosComunicacao":null,"carteiraGRC":null}

{"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.

感谢您的帮助.

推荐答案

我认为这是有问题的,因为您为interface类和Model类使用了相同的名称.

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

在这里都是相同的名称ClientePFApi.

both are same name here ClientePFApi .

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

您可以像下面那样使用ClientePFVisao360

you use ClientePFVisao360 like below

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

这篇关于Android + Retrofit 2 + GSON =无法为接口调用无参数构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆