呼叫 Mobile 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 中的根物件為 MobileManagerMobileManager 物件管理 MobileBackend 物件。

MobileBackend 物件可管理您應用程式及其關聯後端之間的連線、認證及其他交易,包括對平台 api 的呼叫以及您已定義的任何自訂 api。它透過 ServiceProxy 的例項 (例如 StorageLocation) 來管理對平台 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 或空白的端點上呼叫 REST 方法 (GET、PUT、POST 或 DELETE),且回應有效負載為 JSON 或空白。

您可以使用 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);