モバイル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など)を管理します。ここでは、Storage
やLocation
など、ServiceProxy
のインスタンスを介して、プラットフォーム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では、Oracle Mobile Hubでのカスタムapiのコールを簡略化するために、CustomHttpResponse
クラス、GenericCustomCodeClientCallBack
インタフェースおよび認可クラスのinvokeCustomCodeJSONRequest
メソッドが提供されます。リクエスト・ペイロードが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);