モバイルApiのコール

Oracle Mobile Hubのバックエンドとは、アプリケーションで使用できるカスタムapi、ストレージ収集、その他のリソースを論理的にグループ化したものです。バックエンドは、これらのリソースにアクセスするためのセキュリティ・コンテキストも提供します。

Androidアプリケーションでバックエンドを使用するための一般的な手順を次に示します。

  1. クライアントSDKをアプリケーションに追加します。

  2. oracle_mobile_cloud_config.xmlに、バックエンドの環境および認証の詳細を入力します。

  3. 構成情報をロードするためのSDKコールをアプリケーションに追加します。

  4. 認証を処理するためのSDKコールをアプリケーションに追加します。

  5. 使用する他の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など)を管理します。ここでは、StorageLocationなど、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);