android - 如何从 Android 中 Contentful 的空间中检索条目

标签 android contentful

今天刚开始使用这项服务,文档有点困惑。 我使用相册模板创建了一个 Space。在这个空间中,我有图像、作者和照片库类型的条目。我的问题是,如何检索图像以显示在我的 Android 应用程序中?

要与 Contentful Delivery API 交互,我知道我必须使用 CDAClient 对象和 CDAEntry 对象作为我正在尝试的条目得到。

我在 Fragment 类的 onCreate() 方法中定义了它,如下所示:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getActivity();

    client = CDAClient.builder()
            .setSpace("id-of-the-space-am-accessing")
            .setToken("the-access-token")
            .build();

    entry = client.fetch(CDAEntry.class).one("the id of the image I want to retrieve");
}

onCreateView() 方法中,我试图像这样查看结果:

Log.e(TAG, "Result: " + entry.toString());

当我运行应用程序时,我在 Logcat 中得到了这个:

E/NEWS ACTIVITY﹕ Result: CDAEntry{id='2YhtjbebgscIwO2keYEa4O'}

这个 id 与我传递给 client.fetch(CDAEntry.class).one()

的图像的 id 匹配

显然我正在获取图像,但我该如何显示它?

最佳答案

引用official documentation :

内容类型

Content Types are schemes describing the shape of Entries. They mainly consist of a list of fields acting as a blueprint for Entries.

Assets

Assets represent files in a Space. An asset can be any kind of file: an image, a video, an audio file, a PDF or any other filetype. Assets are usually attached to Entries through Links.

条目

Entries are the documents contained within a space. They might represent blog posts or events or anything else that you have defined a content type for.

空间

All content in Contentful belongs to a space. You can think of spaces like databases, you will generally have at least one space for a project, but may opt to use a separate space for e.g. testing.

如上所述,为了包含一些关于它的额外元数据,有一个包装 Assets 的条目是很常见的。例如,考虑 Image 类型,它具有以下字段集:

  • 标题 - 短文本
  • 照片 - Assets 链接
  • imageCaption - 长文本
  • imageCredits - 长文本

使用 Java/Android SDK,可以通过条目的 getField() 方法访问这些属性:

CDAEntry imageEntry = client.fetch(CDAEntry.class).one("foo");
String title = imageEntry.getField("title");
CDAAsset photo = imageEntry.getField("photo");
String photoUrl = photo.url(); // URL of the linked photo
String imageCaption = imageEntry.getField("imageCaption");
String imageCredits = imageEntry.getField("imageCredits");

请注意,getField() 方法会推断返回类型以避免显式转换,因此请确保在调用它时使用正确的类型。

值得一提的是 one(String id) 方法是同步的,会在调用线程上执行 IO,所以你可以使用 one(String id, CDACallback callback) 或者如果您熟悉 RxJava,还有 client.observe() 可以为您提供 Observable

最后要注意的是,在某些情况下,每次创建 Activity/fragment 时从网络获取资源可能被认为是一种浪费,最好将资源保存到本地数据库并在离线模式下访问它。为此,您可能需要查看 Vault ,这是一个注释处理器,可帮助您以最小的努力通过 SQLite 从 Contentful 中保存数据,它使用 Contentful Sync API 从给定空间获取增量更新,从而减少电池和数据消耗,同时还有助于提供更好的离线行为。

Java/Android SDK 的 JavaDoc 在这里:http://contentful.github.io/contentful.java/

还有一些开源示例应用程序展示了 Vault 与空间模板的用法,照片库模板可在此处获得:https://github.com/contentful/gallery-app-android

干杯!

关于android - 如何从 Android 中 Contentful 的空间中检索条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31876237/

相关文章:

android - 使用特定的 Android 系统 WebView

android - 如何将微调器值获取到字符串

reactjs - Gatsby 错误无法读取 null 的属性 'fixed'

javascript - 内容更新时返回 'version mismatch' 的内容 API

Contentful CMS 未正确显示数据

ruby-on-rails - rails delete_if 使用哈希忽略当前文章(中间人)

android - 改造错误预期 BEGIN_ARRAY 但 BEGIN_OBJECT 更改为对象确实对我有用

android - 隐藏 Activity

java - 安卓资源 : How are bitmaps loaded from resources handled memory wise?

javascript - 内容丰富的 Javascript SDK 和 NativeScript