呼叫移动 Api
在Oracle Mobile Hub中,后端是定制 api、存储集合和其他可在应用中使用的资源的逻辑分组。后端还提供了用于访问这些资源的安全上下文。
下面是使用 Android 应用程序中后端的一般步骤:
-
将客户机 SDK 添加到您的应用程序。
-
填写
oracle_mobile_cloud_config.xml
,其中包含后端的环境和验证详细信息。 -
向您的应用程序添加 SDK 调用以加载配置信息。
-
向应用程序添加 SDK 调用以处理验证。
-
添加要使用的任何其他 SDK 调用。
加载后端配置
对于使用 Android 客户机 SDK 成功完成对Oracle Mobile Hub API 的任何调用,您需要从应用程序的 oracle_mobile_cloud_config.xml
文件加载后端配置。可以使用 MobileManager
类执行以下操作:
MobileManager.getManager().getMobileBackend(this)
调用平台 Api
将移动后端的配置信息加载到应用程序中之后,可以调用客户机 SDK 类。
Android SDK 中的根对象为 MobileManager
。MobileManager
对象用于管理 MobileBackend
对象。
MobileBackend
对象管理应用程序及其关联后端之间的连接、验证和其他事务处理,包括对平台 api 的调用和您定义的任何定制 api。它通过 ServiceProxy
实例(例如 Storage
和 Location
)管理对平台 api 的调用。
下面的示例说明了如何使用这些类上载使用存储 API 的图像:
try {
Storage storage = MobileManager.getManager().getMobileBackend(this).getServiceProxy(Storage.class);
StorageCollection imagesCollection = storage.getStorageCollection("FIF_Images");
StorageObject imageToUpload = new StorageObject(null, imageBytes, "image/jpeg");
StorageObject uploadedImage = imagesCollection.post(imageToUpload);
} catch(ServiceProxyException e) {int errorCode = e.getErrorCode();
...
}
创建的 ServiceProxy
实例管理对存储平台 API 的调用,包括用访问 API 所需的移动后端身份证明构造 HTTP 标头。
以及如何使用存储 API 检索映像:
try {
Storage storage = MobileManager.getManager().getMobileBackend(this).getServiceProxy(Storage.class);
StorageCollection imagesCollection = storage.getStorageCollection("FIF_Images");
StorageObject image = imagesCollection.get("3x4mp1e-st0r4g3-0bj3ct-k3y");byte[] imageBytes = image.getPayloadBytes();
} catch(ServiceProxyException e) {int errorCode = e.getErrorCode();
...
}
调用定制 Api
客户机 SDK 提供了授权类中的 CustomHttpResponse
类、GenericCustomCodeClientCallBack
接口和 invokeCustomCodeJSONRequest
方法,用于简化在Oracle Mobile Hub中调用定制 api 的过程。可以对请求有效负载为 JSON 或空且响应有效负载为 JSON 或空的端点调用 REST 方法(GET、PUT、POST 或 DELETE)。
使用 GenericCustomCodeClientCallBack
创建响应的处理程序(以 CustomHttpResponse
对象的形式返回)。
然后,要调用定制 API,可以在 Authorization
对象上调用 invokeCustomCodeJSONRequest(GenericCustomCodeClientCallBack restClientCallback, JSONObject data, String functionName, RestClient.HttpMethod httpMethod)
。
要调用定制 API 端点,可以使用如下内容:
import org.json.JSONObject;
import oracle.cloud.mobile.customcode.CustomHttpResponse;
import oracle.cloud.mobile.customcode.GenericCustomCodeClientCallBack;
import oracle.cloud.mobile.mobilebackend.MobileManager;
.......
final GenericCustomCodeClientCallBack genericCustomCodeClientCallBack = new GenericCustomCodeClientCallBack() {
@Override
public void requestCompleted(CustomHttpResponse response, JSONObject data, Exception e) {
boolean getResponse = (response.getHttpStatus() >=200 && response.getHttpStatus() <300);
// write any logic based on above response
}
};
AuthorizationAgent authorization = MobileManager.getManager().getMobileBackend(this).getAuthorization();
authorization.authenticate(mActivity, "user1", "pass1", successCallback);
........
// after the user successfully authenticates, make a call to the custom API endpoint
authorization.invokeCustomCodeJSONRequest(genericCustomCodeClientCallBack, null, "TaskApi/tasks", RestClient.HttpMethod.GET);