2017-07-15 128 views
0

我想在我的應用程序使用改造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}

我很抱歉,如果它是一個小白問題/問題,但我我在這裏讀了很多問題,不知道我的錯誤在哪裏。

感謝您的任何幫助。

回答

1

我覺得有一個問題,因爲你用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); 
    } 
+0

哦......我知道了...,改變錯誤太多改變,但它似乎JSON的壞格式化,新的錯誤:COM。 google.gson.JsonSyntaxException:java.lang.IllegalStateException:預期的BEGIN_ARRAY,但是在STRING的第1行第4列路徑$ .nichos –

+1

這個錯誤是因爲在你的'ClientePFVisao360'類'nichos'和'carteiraGRC'中是List對象並且在你的' Json'包含該鍵的'null'。爲了解決這個問題,你需要爲這個鍵清空數組''''來代替'null'。 –

相關問題