改造无法为空创建类

时间:2019-07-17 22:17:42

标签: java android retrofit2 android-livedata mutablelivedata

我自定义了改造API,以返回一个livedata对象而不是一个调用对象,以尝试遵循mvvm架构和单一真相原理,然后出现此错误

我已经对pojo类进行了交叉检查,是否有错别字,现在我被困住了。

这是发帖请求

     @Headers({"Content-Type: application/json"}) 
     @POST("api/books")
     LiveData<ApiResponse<BookResponse>> getBooks(@Header("Authorization") 
                                                   String bearerId);

这是实例化改造的类

public class ServiceGenerator {
private static Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(ScalarsConverterFactory.create())
        .addCallAdapterFactory(new LiveDataCallAdapterFactory())
        .addConverterFactory(GsonConverterFactory.create());

private static Retrofit retrofit = retrofitBuilder.build();

private static AkiddieApiLink apiLink = retrofit.create(AkiddieApiLink.class);

public static AkiddieApiLink getApiClient() {
    return apiLink;
}

}

这就是它的称呼方式:

 public LiveData<Resource<List<Data>>> getBooks(final String token) {
    return new NetworkBoundResource<List<Data>, BookResponse>(AppExecutors.getInstance()) {
        @Override
        public void saveCallResult(@NonNull BookResponse item) {
            if (item.getData() != null) {
                Data[] data = new Data[item.getData().size()];
                booksDataDAO.insertBooksData((Data[]) (item.getData().toArray(data)));
            }
        }

        @Override
        public boolean shouldFetch(@Nullable List<Data> data) {
            return true;
        }

        @NonNull
        @Override
        public LiveData<List<Data>> loadFromDb() {
            return booksDataDAO.getAllLocalBooks();
        }

        @NonNull
        @Override
        public LiveData<ApiResponse<BookResponse>> createCall() {
            return ServiceGenerator.getApiClient().getBooks(token);
        }
    }.getAsLiveData();
}

这是我得到的错误:

java.lang.IllegalArgumentException:无法为null创建转换器

0 个答案:

没有答案