Connect native Android app to Watson Sentiment Analysis in under 10 minutes
5 min read
Connect native Android app to Watson Sentiment Analysis in under 10 minutes
This blog post describes how you can add Watson’s cognitive service to an Android app in about 10 minutes. This app was demonstrated at JavaOne 2016 as shown in the following video:
OK, let’s get going!
Prerequisites
-
Download the Android Studio and install it
-
You’ll need either a real Android device (a smartphone or a tablet), or a virtual one (emulator)
-
Signup for IBM Bluemix to get access to IBM Watson cognitive services.
Overview of steps
Below is an overview of the steps:
-
Create a typical Android application in Java
-
Add the Watson lib: Watson-Developer-Cloud SDK for Java
-
Instantiate the Bluemix Watson service and get the key token to it
-
Add some code in your Android app to invoke the cognitive service
-
Quick test of the app.
Step 1: Create a typical Android application in Java.
You’ll build a simple application with a submit button, an editable text field, and an output field.
Idea: Create the user interface first; it’s a simple single view application with a GUI that includes a text field, a label, and a button. When the user presses the button, the text in the text field is sent to the Watson service that will be implemented in the next steps; Watson analyzes it and returns a JSON result that is shown in the output field.
For now, you would take the text in the text field and simply echo it in the label field when the button is pressed.
UI layout: The video below shows the steps, followed by a more detailed explanation:
To begin, launch the Android Studio. You start with the New Project option. Choose the app name, and enter your company domain name. Choose the environment the app will run on (I selected API 22 – Android 5.1 Lollipop). I added an empty activity and kept the defaults for the activity. The remaining steps are as follows:
-
Wait until Gradle builds the environment. When it is done, select directory
res / layout
and double click onactivity_main.xml
-
When the UI designer appears you might want to select the
TextView widget
on the layout designer; the selected text shows “Hello World!” -
Let’s add
Plain TextView
to enter text to be analyzed by Watson Sentiment Analysis service. Double click on the widget to add a value (any test text will do). -
And also let’s add the
Button
to fire an action. Double click on the widget to add a value as the button label.
UI wiring: Get back to the code by selecting java, your appName (with your company domain name) and MainActivity
. You need to double click the activity. Then add these parameters just after the import section in the class definition: textField, editField, button
. It should generate the required imports:
Then fire an action (through OnClickListener
) when the button is pressed:
Now we need to add the AskWatson
task that manages the thread for listener. The text needs to be added just before the onCreate
method:
The selected task will call and fetch the results from Watson (to be implemented in the next sections).
Now we are ready to run the simulator - press the green "PLAY" arrow next to the "APP". Building the app and launching the simulator (or connecting to the device) takes a few moments:
If you click the Analyze! button, you should be able to see the result at the TextView
field. You should also see some output in the console debug.
Get Cognitive!
The video below shows all the steps followed by a more detailed explanation.
Step 2: Add the Watson lib: Watson-Developer-Cloud SDK for Java
Check the detailed installation steps in the Watson Developer Cloud SDK for Java on GitHub.
Alternatively, download the Java library for the SDK (jar) and place it in the lib
folder to get in included for the environment. Specifically, put the downloaded jar file in the libs
folder under the app root directory apps/libs
as follows:
You should be able to see the app in the libs folder in the projects view in the Android Studio. Then double click on the jar to add it to the [app]
as the library. Finally we are able to import those Watson libraries:
Now we need to allow to call Watson service from our app. Double click /app/manifest/AndroidManifest.xml
in the view Android and add the following two lines:
For reference, the following is the entire file content:
Step 3: Instantiate the Bluemix Watson service and get the key token to it.
Next you'll connect your code to the Watson AlchemyAPI service. From the IBM Bluemix catalog, click Watson > AlchemyAPI > Create. Be sure to use a static API key as shown in the following image:
Bluemix provides your credentials in JSON format. The JSON snippet above lists credentials, such as the API token, or a key/secret pair, and connection information for the service.
Step 4: Add some code in your Android app to invoke the cognitive service.
Next you'll add Watson credentials to the code and initialize the Watson Developer Cloud SDK for Java service. We are going to leverage Sentiment Analysis from Watson AlchemyAPI service. First we need to import the required classes:
Then we can expand the AskWatsonTask
private class. First we need to instantiate the AlchemyApi service:
Step 5: Quick test of the app
Do a quick test of your app:
When we run the app in the simulator, and hit the Analyze button, in the UI we see POSITIVE or NEUTRAL or NEGATIVE sentiment of the message. More information about the weight of the result (positive or negative) is shown in the returned JSON from Watson service and printed on the console. An example JSON sentiment result for the input "This is not a nice car" is shown below:
Please follow me on Twitter: @blumareks, and check my blog on blumareks.blogspot.com. For reference, please see this Gist on GitHub for a full listing of the application above.