2017-04-08 19 views
-1

我有这样的基本抽象类。继承与改造未能调用,没有参数

public abstract class Species implements Parcelable { 
    public Species() { 

    } 

    public abstract String name(); 
} 

然后我的人类看起来像这样。

@AutoValue 
public abstract class Human extends Species implements Parcelable { 
    public static Human create(String humanVariable) { 
     return new AutoValue_Human(humanVariable); 
    } 

    public static Human create(String name, String humanVariable) { 
     return new AutoValue_Human(name, humanVariable); 
    } 

    public static TypeAdapter<Human> typeAdapter(Gson gson) { 
     return new AutoValue_Human.GsonTypeAdapter(gson); 
    } 

    @SerializedName("name") 
    public abstract String name(); 

    @Nullable 
    @SerializedName("human_variable") 
    public abstract String humanVariable(); 

} 

E/com.service.androidbrain.util.networking.RetrofitCallback.onFailure: 失败,但没有 参数传递给调用公共com.service.example.models.Species()

由于某种原因,但我得到这个错误,我不明白发生了什么,有什么想法?

+0

您是否添加了基于'Gson'配置的'GsonConverterFactory'在构建'Retrofit'实例时,我在您之前的问题中暗示您? –

+0

是的,我有,不知道发生了什么,我可能会错过一些东西。 – Claud

+0

您能否提供构建Gson和Retrofit实例的代码? –

回答

0

您应该更喜欢构图而不是继承,因为至少目前在AutoValue中没有这样做。

在github上查看this issue

2

auto-value-gson不支持您尝试实现的行为。

我假设你宣布你的改造服务返回Call<Species>,而只有Human.typeAdapter(Gson)被注册到Gson。 Gson的结论并不知道如何创建Species的实例。

要做到这一点,您必须(创建并)安装Species的另一个类型适配器,该适配器知道如何识别物种的实际子类型,并将所有模型创建委托给特定类型的适配器。

2

我相信你只是没有正确实例化GsonConverterFactory。从my answer你前面的问题:

@GsonTypeAdapterFactory 
abstract class HumanAdapterFactory 
     implements TypeAdapterFactory { 

    public static TypeAdapterFactory create() { 
     return new AutoValueGson_HumanAdapterFactory(); 
    } 

} 

因此,以下是不必要的:

public static TypeAdapter<Human> typeAdapter(Gson gson) { 
    return new AutoValue_Human.GsonTypeAdapter(gson); 
} 

所以,你只需要实例Gson

new GsonBuilder() 
     ... 
     .registerTypeAdapterFactory(HumanAdapterFactory.create()) 
     .create(); 

配置的改造实例这个:

new Retrofit.Builder() 
     ... 
     .addConverterFactory(GsonConverterFactory.create(gson)) // THIS is necessary 
     .build(); 

确保您的服务接口运行在HumanSpecies

interface IService { 

    @GET("/") // This is probably what you really want 
    Call<Human> getHuman(); 

    @GET("/") // How can you know WHAT the Species is here? 
    Call<Species> getSpecies(); 

} 

如果你真的想要去与getSpecies(),你必须知道什么是真正的类型的Species接口为特定对象:所以你要么使用InstanceCreator检测一些信息的真实类型。这两种方法在我的答案中都有描述。为了使其工作:

final Gson gson = new GsonBuilder() 
     .registerTypeAdapterFactory(HumanAdapterFactory.create()) 
     .registerTypeAdapterFactory(new TypeAdapterFactory() { 
      @Override 
      public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) { 
       // ... that big type adapter here from that answer OR InstanceCreator by it's not useful here 
      } 
     }) 
     .create(); 

final Retrofit retrofit = new Retrofit.Builder() 
     ... 
     .addConverterFactory(GsonConverterFactory.create(gson)) 
     .build(); 

final IService service = retrofit.create(IService.class); 
final Species species = service.getSpecies() 
     .execute() 
     .body(); 
System.out.println(species.getClass()); 
System.out.println(species.name()); 

这就是你所需要的,但Call<Human> getHuman();是最好的选择这里。