我想在我的应用程序使用改造2和我得到的folloing错误:的Android +改造2 + GSON =无法调用无参数的构造函数接口
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:
{"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}
我很抱歉,如果它是一个小白问题/问题,但我我在这里读了很多问题,不知道我的错误在哪里。
感谢您的任何帮助。
哦......我知道了...,改变错误太多改变,但它似乎JSON的坏格式化,新的错误:COM。 google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期的BEGIN_ARRAY,但是在STRING的第1行第4列路径$ .nichos –
这个错误是因为在你的'ClientePFVisao360'类'nichos'和'carteiraGRC'中是List对象并且在你的' Json'包含该键的'null'。为了解决这个问题,你需要为这个键清空数组''''来代替'null'。 –