X

@OracleIMC Partner Resources & Training: Discover your Modernization options + Reach new potential through Innovation

Consuming MCS Custom API using SDK for Android

Guest Author

Recently experimenting with the latest Oracle Mobile Cloud Service SDK for
Android I was pleasantly surprised how easy you can call MCS Custom
API
using SDK with just a few lines of code. In common case scenario to
make the simplest GET request to REST service you most likely will use HttpURLConnection
class to create connection and make a request and Uri.Builder
helper class for building or manipulating URI references etc. In addition to
this you will need to define Oracle-Mobile-Backend-ID
and Authorization request headers since
MCS Mobile
Backend
does not support fully anonymous calls. You can refer to this documentation
article
for further details about testing your Custom API outside MCS
console. But good news, that MCS SDK greatly simplifies this work and allows
making Custom API calls with the just few lines of code.

So let’s go into details. Provided code simple snippet below is based on MCS SDK for Android and explains how to make typical
GET request to Custom API REST endpoint.

// Required import

import
org.json.JSONObject;

import
oracle.cloud.mobile.customcode.CustomHttpResponse;

import
oracle.cloud.mobile.customcode.GenericCustomCodeClientCallBack;

oracle.cloud.mobile.mobilebackend.MobileBackendManager;

...

// GenericCustomCodeClientCallBack
callback

final
GenericCustomCodeClientCallBack genericCustomCodeClientCallBack = new GenericCustomCodeClientCallBack()
{

// Implement
requestCompleted method to process request results

@Override

  public void
requestCompleted(CustomHttpResponse response, JSONObject data, Exception e) {

boolean getResponse =
(response.getHttpStatus() >=200 && response.getHttpStatus()
<300);

        // Define any business
logic to process request here

       }

  };

// Obtain
AuthorizationAgent object

AuthorizationAgent
authorization =
MobileBackendManager.getManager().getDefaultMobileBackend(mActivity).getAuthorization();

/* Before invoking Custom API endpoints you would need to perform user
authentication in any point of your app where authentication is guaranteed (e.g. in LoginActivity). You can use authenticate method of 
AuthorizationAgent passing username, password and asynchronous callback. You can find example below in this comment block. For more information please refer to official documentation: http://bit.ly/29RPg5s

... 

try {

    MobileBackendManager mobileBackendManager = MobileBackendManager.getManager();

    MobileBackend mobileBackend = mobileBackendManager.getDefaultMobileBackend(this);

    Authorization authorization = mobileBackend.getAuthorization();

    authorization.authenticate(this, email, password, callback);

  } catch (ServiceProxyException e) {

    Log.d("Exception", "ServiceProxyException");

  }

 ...

 private AuthorizationCallback callback = new AuthorizationCallback() {

  @Override

  public void onCompletion(ServiceProxyException e) {

    showProgress(false);

    if (e == null) {

      Log.d("Success", "Login success"); // Handle successful login

    } else {

      Log.d("Failure", "Login failed");  // Handle failed login

    }

  }

};

*/ 

/* Use
invokeCustomCodeJSONRequest method to call a REST GET method on
mbcards/paymentcards/user/ endpoint to get response in JSON format. 
We also pass here
GenericCustomCodeClientCallBack callback to process results of the request.*/

authorization.invokeCustomCodeJSONRequest(genericCustomCodeClientCallBack,
null, "mbcards/paymentcards/user", RestClient.HttpMethod.GET);

For further
information please check out Android SDK
Reference for Oracle Mobile Cloud Service
and Android
Application
section in official documentation.

During your
mobile app development most likely along with calling Custom API you will need
to implement at least user authentication and authorization and register user
device to send push notifications. This is also covered by APIs provided in MCS
SDK for different platforms. So I strongly encourage you to visit Oracle Mobile
Cloud YouTube Channel
and checkout short Mobile
Cloud Service videos
which describe how to use MCS SDK for different
platforms in details.

Stay tuned with us!

Facebook Google+ Twitter LinkedIn Pinterest Delicious Digg Addthis

Join the discussion

Comments ( 2 )
  • steve Wednesday, July 13, 2016

    I believe the authorization.authenticate call is asynchronous. So, if you call authorization.invokeCustomCodeJSONRequest immediately after the call to authenticate, likely it won't have authenticated at the time of the invokeCustomCodeJSONRequest. I believe you would want to put this method call elsewhere where you will be guaranteed that it has already performed authentication.


  • Emin Askerov Thursday, July 14, 2016

    Steve,

    Thank you for comment. You are right, the authentication call is asynchronous. I know this, but this code snippet is a bit schematic. But to clarify things I've added a comment block with the sample which clarifies the authentication part.

    Thank you.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha