我有一个与RxJava结合使用的Retrofit界面。我所有的改装调用都返回Observable。那些“SomePojo”类,我使用Schema2Pojo站点在线生成它们。
进行以下api调用时遇到问题: https://developers.themoviedb.org/3/search/2Y9y2LReFZdHFHbFA
如您所见,它是一个包含两种不同类型对象的数组,我称之为“Media”和“Credit”。我使用Google的autovalue生成的这两个类如下:
@AutoValue
public abstract class Media implements Parcelable {
@SerializedName(value = "title", alternate = {"name"})
public abstract String title();
@Nullable
@SerializedName("vote_average")
public abstract String voteAverage();
@Nullable
@SerializedName("backdrop_path")
public abstract String backdropPath();
@Nullable
public abstract String adult();
public abstract String id();
@Nullable
public abstract String overview();
@Nullable
@SerializedName("original_language")
public abstract String originalLanguage();
@Nullable
@SerializedName("genre_ids")
public abstract List<String> genreIds();
@Nullable
@SerializedName(value = "release_date", alternate = {"first_air_date"})
public abstract String releaseDate();
@Nullable
@SerializedName(value = "original_title", alternate = {"original_name"})
public abstract String originalTitle();
@Nullable
@SerializedName("vote_count")
public abstract String voteCount();
@Nullable
@SerializedName("poster_path")
public abstract String posterPath();
@Nullable
public abstract String video();
@Nullable
@SerializedName("media_type")
public abstract String mediaType();
@Nullable
public abstract String popularity();
@Nullable
@SerializedName("origin_country")
public abstract List<String> originalCountry();
public static Media create(String title, String voteAverage, String backdropPath,
String adult, String id, String overview, String originalLanguage,
List<String> genreIds, String releaseDate, String originalTitle,
String voteCount, String posterPath, String video, String mediaType,
String popularity, List<String> originalCountry) {
return new AutoValue_Media(title, voteAverage, backdropPath, adult, id, overview,
originalLanguage, genreIds, releaseDate, originalTitle, voteCount, posterPath,
video, mediaType, popularity, originalCountry);
}
public static TypeAdapter<Media> typeAdapter(Gson gson) {
return new AutoValue_Media.GsonTypeAdapter(gson);
}
}
和
@AutoValue
public abstract class Credit implements Parcelable {
public abstract String id();
@SerializedName("credit_id")
public abstract String creditId();
@Nullable
public abstract String department();
public abstract String name();
@Nullable
@SerializedName(value = "job", alternate = {"character"})
public abstract String job();
@Nullable
@SerializedName("profile_path")
public abstract String profilePath();
@Nullable
public abstract String order();
@Nullable
@SerializedName("cast_id")
public abstract String castId();
public static Credit create(String id, String creditId, String department, String name, String job,
String profilePath, String order, String castId) {
return new AutoValue_Credit(id, creditId, department, name, job, profilePath, order, castId);
}
public static TypeAdapter<Credit> typeAdapter(Gson gson) {
return new AutoValue_Credit.GsonTypeAdapter(gson);
}
}
为了解决数组使用两种不同类型的对象创建的问题,我通过此调用实现了POJO返回它自己的JsonDeserializer:
public class MediaListPojo {
@SerializedName("results")
private List<Media> movies;
private List<Credit> credits;
private Dates dates;
private String page;
private String total_pages;
private String total_results;
public List<Media> getMedia() {
return movies;
}
public void setMovies(List<Media> movies) {
this.movies = movies;
}
public List<Credit> getCredits() {return credits;}
public void setCredits(List<Credit> credits) {this.credits = credits;}
public Dates getDates() {
return dates;
}
public void setDates(Dates dates) {
this.dates = dates;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String getTotal_pages() {
return total_pages;
}
public void setTotal_pages(String total_pages) {
this.total_pages = total_pages;
}
public String getTotal_results() {
return total_results;
}
public void setTotal_results(String total_results) {
this.total_results = total_results;
}
@Override
public String toString() {
return "MediaListPojo{" +
"movies=" + movies +
", credits=" + credits +
", dates=" + dates +
", page='" + page + '\'' +
", total_pages='" + total_pages + '\'' +
", total_results='" + total_results + '\'' +
'}';
}
public static class MediaListPojoDeserializer implements JsonDeserializer<MediaListPojo> {
@Override
public MediaListPojo deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
MediaListPojo mediaListPojo = new Gson().fromJson(json, MediaListPojo.class);
JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("results")) {
JsonArray jsonArray = jsonObject.getAsJsonArray("results");
List<Credit> credits = new ArrayList<>();
Credit credit;
for (JsonElement element : jsonArray) {
JsonObject current = element.getAsJsonObject();
if (current.get("media_type").getAsString().equals("person")) {
credit = new Gson().fromJson(current, Credit.class);
credits.add(credit);
}
}
mediaListPojo.setCredits(credits);
}
return mediaListPojo;
}
}
}
这个json反序列化器背后的主要思想是:“对这个类使用默认类型适配器,然后使用这个JsonDeserializer设置Credit对象”
但是,出于某种原因,我在反序列化时遇到以下错误:
java.lang.RuntimeException:无法调用没有args的公共Media() ... 引起:java.lang.InstantiationException:无法实例化抽象类Media at java.lang.reflect.Constructor.newInstance(Native Method)
它不应该尝试实例化抽象超类,而是使用AutoValue生成的Type Adapter。
这就是我构建改造实例的方式:
class Creator {
public static MovieService newMovieService() {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new AutoValueGson_MyAdapterFactory())
.registerTypeAdapter(MediaListPojo.class, new MediaListPojo.MediaListPojoDeserializer())
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
.create();
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(NetworkUtil.makeQueryInterceptor("api_key", BuildConfig.MY_API_KEY))
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(MovieService.ENDPOINT)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit.create(MovieService.class);
你能帮我理解我做错了吗?
答案 0 :(得分:3)
嗯,我在发布问题后5分钟找到了解决方案,但是因为我认为其他人也可能会对此感到困惑。我将分享解决方案:
基本上,在我的JsonDeserializer中,我使用的是Gson对象的新实例,实际上这是一个错误。
我在创建改造实例时注册的typeadapterfactory是所有其他TypeAdapter的所在。
因此,请致电
Gson gson = new Gson();
不提供我需要的类型适配器来反序列化对象的其余部分。
我希望它有所帮助。
答案 1 :(得分:1)
在创建Gson实例时,您需要像这样注册TypeAdapterFactory。对我来说,我在下面的匕首2模块中做到了这一点。
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(GsonAdapterFactory.create())
.create();
希望这会有所帮助。