Call Mobile APIs
In Oracle Mobile Hub, a backend is a logical grouping of custom APIs, storage collections, and other resources that you can use in your apps. The backend also provides the security context for accessing those resources.
Here are the general steps for using a backend in your Android app:
-
Add the client SDK to your app.
-
Fill in the
oracle_mobile_cloud_config.xml
with environment and authentication details for the backend. -
Add an SDK call to your app to load the configuration info.
-
Add an SDK call to your app to handle authentication.
-
Add any other SDK calls that you want to use.
Load the Backend's Configuration
For any calls to Oracle Mobile Hub APIs using the Android client SDK to successfully complete, you need to have the
backend’s configuration loaded from the app’s
oracle_mobile_cloud_config.xml
file. You do this using the
MobileManager
class:
MobileManager.getManager().getMobileBackend(this)
Call Platform APIs
Once the mobile backend’s configuration info is loaded into the app, you can make calls to client SDK classes.
The root object in the Android SDK is MobileManager
. The MobileManager
object manages MobileBackend
objects.
The MobileBackend
object manages connectivity, authentication, and other transactions between your application and its associated backend, including calls to platform APIs and any custom APIs you have defined. It manages calls to platform APIs through instances of ServiceProxy
such as Storage
and Location
.
Here’s an example of how you would use these classes to upload an image using the Storage 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();
...
}
The ServiceProxy
instance created there manages calls to the Storage platform API, including the constructing of the HTTP headers with the mobile backend credentials necessary to access the API.
And here’s how you could retrieve an image using the Storage 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();
...
}
Call Custom APIs
The client SDK provides the CustomHttpResponse
class, the
GenericCustomCodeClientCallBack
interface, and the
invokeCustomCodeJSONRequest
method in the authorization classes to
simplify the calling of custom APIs in Oracle Mobile Hub. You can call a REST method (GET, PUT, POST, or DELETE) on an endpoint where the
request payload is JSON or empty and the response payload is JSON or empty.
You use GenericCustomCodeClientCallBack
to create a handler for the response (which is returned in the form of a CustomHttpResponse
object.)
Then, to call the custom API, you call invokeCustomCodeJSONRequest(GenericCustomCodeClientCallBack restClientCallback, JSONObject data, String functionName, RestClient.HttpMethod httpMethod)
on your Authorization
object.
To make a call to a custom API endpoint, you could use something like this:
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);