如何设置具有自动值的ImmutableList?

时间:2017-01-20 09:46:03

标签: android retrofit2 auto-value immutable-collections

我尝试使用自动值设置不可变列表。我遵循了官方用户指南,建议实施类似的东西:

@AutoValue
public abstract class ListExample {
  public static ListExample create(String[] mutableNames) {
    return new AutoValue_ListExample(ImmutableList.copyOf(mutableNames));
  }

  public abstract ImmutableList<String> names();
}

我的实施是下一个:

@AutoValue
public abstract class MovieList {

    @SerializedName("results")
    //if a change ImmutableList to simple List right below
    //the Retrofit call works and my Object is printed.
    public abstract ImmutableList<Movie> movies();

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

我想循环播放MovieList对象的电影,作为改装电话的响应。

movieService.getPopularMovies().enqueue(new Callback<MovieList>() {
                @Override
                public void onResponse(Call<MovieList> call, Response<MovieList> response) {
                    if(response.isSuccessful()){

                        for(Movie m: response.body().movies()){
                            Timber.e(m.originalTitle());
                        }
                    }
                }

                @Override
                public void onFailure(Call<MovieList> call, Throwable t) {
                    Timber.e(t.toString());
                }
            });

上面的Retrofit调用会导致以下堆栈跟踪:

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/collect/ImmutableList;
    at com.oviesm.models.AutoValue_MovieList$GsonTypeAdapter.<init>(AutoValue_MovieList.java:21)
    at com.oviesm.models.MovieList.typeAdapter(MovieList.java:24)
    at com.oviesm.dependencies.factories.AutoValueGson_MyAdapterFactory.create(AutoValueGson_MyAdapterFactory.java:19)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:63)
    at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:325)
    at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:308)
    at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:704)
    at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:167)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
    at java.lang.reflect.Proxy.invoke(Proxy.java:393)
    at $Proxy0.getPopularMovies(Unknown Source)
    at com.oviesm.activities.MainActivity.onCreate(MainActivity.java:40)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.common.collect.ImmutableList" on path: DexPathList[[dex file "/data/data/com.oviesm/files/instant-run/dex/slice-support-annotations-24.2.1_c58c21259b49dee274cade92b4da707c59aa2c82-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-retrofit-2.1.0_691a200d4fb928332b4740a763ecb5b64166c64d-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-picasso2-okhttp3-downloader-1.1.0_3fe320104c8be5d03d13617741bd1e383fe7aa3e-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-picasso-2.5.2_e72c8c2e003fe3874e0b925f9d1aa0b21f66055d-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-okio-1.11.0_79ac733fa889ba614e6d38e4a65b631b37417580-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-okhttp-3.5.0_2a1e0e3ae2bf6a5168289158a20878f9e1e13d11-classes.dex", dex file "/data/data/com.oviesm/files/instant-run/dex/slice-logging-interceptor-3.5.0_7622cecaaef176468f6389f00c5a52b29da046d5-classes.dex", dex file "/data/data

如果在上面的MovieList抽象类中将ImmutableList更改为simple List,则可以使用Retrofit调用并打印我的Object。是否有一些依赖添加?我的ImmutableList现在从com.google.common.collect.ImmutableList导入。

我应该如何设置我的ImmutableList依赖项才能循环播放MovieList对象?

0 个答案:

没有答案