本教程的第7章使用本机Android移动设备应用程序从AEM Content Services中使用内容。
本教程使用 简单的本机Android移动设备应用程序 以使用和显示由AEM Content Services公开的事件内容。
的使用 Android 基本上不重要,消费性移动应用程序可以在任何框架中编写,适用于任何移动平台,例如iOS。
Android用于教程,因为能够在Windows、macOs和Linux上运行Android模拟器,其受欢迎程度,并且可以以Java(AEM开发人员非常了解的一种语言)编写。
本教程的Android移动设备应用程序是not旨在指导如何构建Android Mobile应用程序或传达Android开发最佳实践,但是却是为了说明如何从移动设备应用程序中使用AEM Content Services。
如果未在上运行AEM发布 http://localhost:4503 可以在移动设备应用程序的 Settings 指向属性AEM发布主机/端口。
此部分重点介绍最能交互且依赖于AEM Content Services及其JSON输出的Android移动设备应用程序代码。
加载后,移动设备应用程序将 HTTP GET
to /content/wknd-mobile/en/api/events.model.json
即AEM Content Services端点,配置为提供内容以驱动移动设备应用程序。
因为事件API的可编辑模板(/content/wknd-mobile/en/api/events.model.json
),则可以对移动设备应用程序进行编码,以在JSON响应中的特定位置查找特定信息。
HTTP GET
请求AEM在以下位置发布 /content/wknd-mobile/en/api/events.model.json
以收集用于填充移动设备应用程序UI的内容。以下是移动应用程序中代码的提取 MainActivity
负责调用AEM Content Services来收集驱动移动设备应用程序体验的内容。
protected void onCreate(Bundle savedInstanceState) {
...
// Create the custom objects that will map the JSON -> POJO -> View elements
final List<ViewBinder> viewBinders = new ArrayList<>();
viewBinders.add(new LogoViewBinder(this, getAemHost(), (ImageView) findViewById(R.id.logo)));
viewBinders.add(new TagLineViewBinder(this, (TextView) findViewById(R.id.tagLine)));
viewBinders.add(new EventsViewBinder(this, getAemHost(), (RecyclerView) findViewById(R.id.eventsList)));
...
initApp(viewBinders);
}
private void initApp(final List<ViewBinder> viewBinders) {
final String aemUrl = getAemUrl(); // -> http://localhost:4503/content/wknd-mobile/en/api/events.mobile.json
JsonObjectRequest request = new JsonObjectRequest(aemUrl, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
for (final ViewBinder viewBinder : viewBinders) {
viewBinder.bind(response);
}
}
},
...
);
}
onCreate(..)
是移动设备应用程序的初始化挂接,并注册3个自定义 ViewBinders
负责解析JSON并将值绑定到 View
元素。
initApp(...)
随后将调用,以便在AEM发布中向AEM Content Services端点发出HTTPGET请求,以收集内容。 收到有效的JSON响应后,JSON响应将传递到每个 ViewBinder
负责解析JSON并将其绑定到移动设备 View
元素。
接下来,我们将查看 LogoViewBinder
,虽然这很简单,但也强调了一些重要注意事项。
public class LogoViewBinder implements ViewBinder {
...
public void bind(JSONObject jsonResponse) throws JSONException, IOException {
final JSONObject components = jsonResponse.getJSONObject(":items").getJSONObject("root").getJSONObject(":items");
if (components.has("image")) {
final Image image = objectMapper.readValue(components.getJSONObject("image").toString(), Image.class);
final String imageUrl = aemHost + image.getSrc();
Glide.with(context).load(imageUrl).into(logo);
} else {
Log.d("WKND", "Missing Logo");
}
}
}
第一行 bind(...)
通过键向下导航JSON响应 :项→根→ :项 它表示已添加组件的AEM布局容器。
从此处将检查名为 图像,表示图像组件(同样,此节点名称→ JSON键值是稳定的)。 如果此对象存在,则会读取并映射到 自定义图像POJO 通过杰克逊 ObjectMapper
库。 图像POJO将在下面进行探索。
最后,标志的 src
会使用 Glide 帮助程序库。
请注意,我们必须提供AEM架构、主机和端口(通过 aemHost
)到AEM发布实例,因为AEM Content Services将仅提供JCR路径(即 /content/dam/wknd-mobile/images/wknd-logo.png
)到引用的内容。
但是,使用的是 Jackson ObjectMapper 或Gson等其他库提供的类似功能,有助于将复杂的JSON结构映射到Java POJO,而无需直接处理本机JSON对象本身。 在这个简单的例子中,我们将 src
键 image
JSON对象,到 src
属性(通过 @JSONProperty
注释。
package com.adobe.aem.guides.wknd.mobile.android.models;
import com.fasterxml.jackson.annotation.JsonProperty;
public class Image {
@JsonProperty(value = "src")
private String src;
public String getSrc() {
return src;
}
}
事件POJO需要从JSON对象中选择更多数据点,它比简单的图像更能从中受益,我们只想从 src
.
现在,您已经了解AEM Content Services如何驱动本机移动设备体验,接下来可以使用您学到的知识执行以下步骤,并查看移动设备应用程序中反映的更改。
在每个步骤之后,通过提取来刷新移动设备应用程序,并验证移动设备体验的更新。
您已完成AEM Headless教程!
要进一步了解AEM内容服务和AEM as a Headless CMS,请访问Adobe的其他文档和支持材料: