java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap无法转换为com.google.api.services.youtube.YouTube $ Videos $ List

时间:2016-08-03 15:13:36

标签: android youtube-api retrofit2 rx-android

LogCat:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.google.api.services.youtube.YouTube$Videos$List

例外: onError在下面的代码中调用

    IService mService = application.getIService(Constants.mYoutubeUrl);
subscription = mService.getYoutubeVideos(Constants.mYoutubeKey ,Constants.mYoutubeChannelId )
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(application.defaultSubscribeScheduler())
                .subscribe(new Observer<YouTube.Videos.List>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        videoMvpView.showMessage(R.string.probleme_connection);
                    }

                    @Override
                    public void onNext(YouTube.Videos.List videos) {
                        videoMvpView.showVideos(videos);
                        videoMvpView.showSelectedVideo(((Video)videos.get(0)).getSnippet().getTitle());
                    }
                });

IService.java

public interface IService {
@GET("/youtube/v3/search?part=snippet,id&order=date&maxResults=20")
Observable<YouTube.Videos.List> getYoutubeVideos(@Query("key") String key, @Query("channelId") String channelId);


class Factory {
    public static IService create(String baseUrl) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit.create(IService.class);
    }
}

}

1 个答案:

答案 0 :(得分:0)

您是否必须使用 YouTube.Videos.List 类,如果不是,则使用json2schema将原始字符串响应转换为pojo,然后尝试使用它,这可能会有帮助,如果您需要使用而不是检查 YouTube.Videos.List 类的结构,如果您可以提供原始响应,则可能有助于解决问题