改造:如何仅为某些API调用设置自定义反序列化器?

时间:2015-06-29 13:22:25

标签: retrofit

大家好,我已经看到这个例子几乎到处都是他们编写自定义GSON反序列化器然后将其添加到RestAdapter

基本上就像这样

 public RestClient()
{
    Gson gson = new GsonBuilder()
            .registerTypeAdapterFactory(new ItemTypeAdapterFactory()) // This is the important line ;)
            .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
            .create();

    RestAdapter restAdapter = new RestAdapter.Builder()
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setEndpoint(BASE_URL)
            .setConverter(new GsonConverter(gson))
            .setRequestInterceptor(new SessionRequestInterceptor())
            .build();

    apiService = restAdapter.create(ApiService.class);
}

但是,如果我的API服务无法对所有API调用使用相同的反序列化器,该怎么办?如果我只想要一个API调用来使用特定的反序列化器,该怎么办?

1 个答案:

答案 0 :(得分:0)

据我所知,在某些测试中,您不得不创建两个不同的RestAdapter实例,一个使用Gson转换器,另一个不使用它。