在 Moshi 中使用 RealmList 时出错

Error using RealmList with Moshi

Caused by: java.lang.IllegalArgumentException: Platform java.lang.Class annotated [] requires explicit JsonAdapter to be registered

在 Moshi 中使用 RealmList 时出现上述错误。

public class MenuModel implements RealmModel
{
    @Json(name = "menugroups")
    private RealmList<MenuGroupModel> menugroups = null;
}

我对此进行了一些研究,发现了以下内容:

https://github.com/realm/realm-java/issues/2711

那里的解决方案说我需要使用 RealmListAdapter。我已将代码复制到我的项目中。但是,我不知道如何以及在何处实现此适配器。

如何使用?

转到这里: Regalia Moshi

RealmListAdapterRealmListAdapterJsonFactory 复制到您的项目(或使用链接的项目)。像这样将适配器添加到您的 moshi:

Moshi moshi = new Moshi.Builder()
.add(new RealmListJsonAdapterFactory())
.build();

更多用法,如果您使用的是 Retrofit:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API.ENDPOINT)
            .addConverterFactory(MoshiConverterFactory.create(moshi))
            .build();