Android end-to-end demonstration
improve this page | report issueOverview
The purpose of this demonstration is to experience an end-to-end flow:
- A sample application that is pre-bundled with the MobileFirst client SDK is registered and downloaded from the MobileFirst Operations Console.
- A new or provided adapter is deployed to the MobileFirst Operations Console.
- The application logic is changed to make a resource request.
End result:
- Successfully pinging the MobileFirst Server.
- Successfully retrieving data using an adapter.
Prerequisites:
- Android Studio
- Optional. MobileFirst CLI (download)
- Optional. Stand-alone MobileFirst Server (download)
1. Starting the MobileFirst Server
Make sure you have created a Mobile Foundation instance, or
If using the MobileFirst Developer Kit, navigate to the server’s folder and run the command: ./run.sh
in Mac and Linux or run.cmd
in Windows.
2. Creating an application
In a browser window, open the MobileFirst Operations Console by loading the URL: http://your-server-host:server-port/mfpconsole
. If running locally, use: http://localhost:9080/mfpconsole. The username/password are admin/admin.
- Click the New button next to Applications
- Select the Android platform
- Enter com.ibm.mfpstarterandroid as the application identifier
- Enter 1.0 as the version value
- Click on Register application
-
Click on the Get Starter Code tile and select to download the Android sample application.
3. Editing application logic
-
Open the Android Studio project and import the project.
-
From the Project sidebar menu, select the app → java → com.ibm.mfpstarterandroid → ServerConnectActivity.java file and:
-
Add the following imports:
import java.net.URI; import java.net.URISyntaxException; import android.util.Log;
-
Paste the following code snippet, replacing the call to
WLAuthorizationManager.getInstance().obtainAccessToken
:WLAuthorizationManager.getInstance().obtainAccessToken("", new WLAccessTokenListener() { @Override public void onSuccess(AccessToken token) { System.out.println("Received the following access token value: " + token); runOnUiThread(new Runnable() { @Override public void run() { titleLabel.setText("Yay!"); connectionStatusLabel.setText("Connected to MobileFirst Server"); } }); URI adapterPath = null; try { adapterPath = new URI("/adapters/javaAdapter/resource/greet"); } catch (URISyntaxException e) { e.printStackTrace(); } WLResourceRequest request = new WLResourceRequest(adapterPath, WLResourceRequest.GET); request.setQueryParameter("name","world"); request.send(new WLResponseListener() { @Override public void onSuccess(WLResponse wlResponse) { // Will print "Hello world" in LogCat. Log.i("MobileFirst Quick Start", "Success: " + wlResponse.getResponseText()); } @Override public void onFailure(WLFailResponse wlFailResponse) { Log.i("MobileFirst Quick Start", "Failure: " + wlFailResponse.getErrorMsg()); } }); } @Override public void onFailure(WLFailResponse wlFailResponse) { System.out.println("Did not receive an access token from server: " + wlFailResponse.getErrorMsg()); runOnUiThread(new Runnable() { @Override public void run() { titleLabel.setText("Bummer..."); connectionStatusLabel.setText("Failed to connect to MobileFirst Server"); } }); } });
4. Deploy an adapter
Download this prepared .adapter artifact and deploy it from the MobileFirst Operations Console using the Actions → Deploy adapter action.
Alternatively, click the New button next to Adapters.
-
Select the Actions → Download sample option. Download the “Hello World” Java adapter sample.
If Maven and MobileFirst CLI are not installed, follow the on-screen Set up your development environment instructions.
-
From a Command-line window, navigate to the adapter’s Maven project root folder and run the command:
mfpdev adapter build
-
When the build finishes, deploy it from the MobileFirst Operations Console using the Actions → Deploy adapter action. The adapter can be found in the [adapter]/target folder.
5. Testing the application
- In Android Studio, from the Project sidebar menu, select the app → src → main →assets → mfpclient.properties file and edit the protocol, host and port properties with the correct values for your MobileFirst Server.
- If using a local MobileFirst Server, the values are typically http, localhost and 9080.
- If using a remote MobileFirst Server (on IBM Cloud), the values are typically https, your-server-address and 443.
- If using a Kubernetes cluster on IBM Cloud Private and if the deployment is of type NodePort, the value of the port would typically be the NodePort exposed by the service in Kubernetes cluster.
Alternatively, if you have installed the MobileFirst CLI, then navigate to the project root folder and run the command
mfpdev app register
. If a remote MobileFirst Server is used, run the commandmfpdev server add
to add the server, followed by for example:mfpdev app register myIBMCloudServer
. - Click on the Run App button.
Results
- Clicking the Ping MobileFirst Server button will display Connected to MobileFirst Server.
- If the application was able to connect to the MobileFirst Server, a resource request call using the deployed Java adapter will take place.
The adapter response is then printed in Android Studio’s LogCat view.
Next steps
Learn more on using adapters in applications, and how to integrate additional services such as Push Notifications, using the MobileFirst security framework and more:
- Review the Developing Applications tutorials
- Review the Adapters development tutorials
- Review the Authentication and security tutorials
- Review the Notifications tutorials
- Review All Tutorials
Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.