Send events from Android app to Microsoft Azure Event Hubs

Event Hubs is a highly scalable publish-subscribe ingestion system that can process millions of events per second. This article will explain how to publish event from an Android application to Azure event hub.

Create an Event Hub

  1. Log on to the Azure management portal, and click NEW at the bottom of the screen.
  2. Click App Services, then Service Bus, then Event Hub, then Quick Create.create_eventhub
  3. Enter the event hub name (eg:- AndroidHub) , select the region and namespace, and create the event hub.
  4. Select the newly created event hub, and select the Configure tab
  5. Create the shared access policy (eg:- android). We will require the policy name, key, namespace and event hub name at later stage to create the signature.shared_access_policy

Create Android application

Let us create anew_project simple android application using Android Studio. Select the minimum SDK (eg:- API 19), Blank Activity template, and  default activity (eg:- MainActivity), and then click “finish”to create the project.

Now add a button to the main layout to send sample event to Azure event hub.  Updated layout is given below.

We need to update the manifest file to support the network operation by including the following permission.

Connecting to EventHub:- Sending events to an Event Hub is accomplished either using HTTP POST or via an AMQP 1.0 connection. The choice of which to use when depends on the specific scenario being addressed. AMQP 1.0 connections are metered as brokered connections in Service Bus and are more appropriate in scenarios with frequent higher message volumes and lower latency requirements as they provide a persistent messaging channel. I will be using the HTTP post to publish the events.  I have been using the Android AsyncHttpClient library for a while to do asynchronous operation, and thought of using the same here as well. Let us update the build.gradle file with the following to add the AsyncHttpClient package.

Now update the button click handler with the following code.

Replace the namespace and eventhub name with the actual values from the Azure portal, and ClientId can be anything to identify the publisher. In real life scenario there will be multiple devices/apps publishing events to same event hub.   I have created another Azure website to generate the SAS signature, and let us use this site to create signature for this demo application.  In production application, it is better to generate the signature in Android app itself (or get it from a secure cloud service).

Once we update all the placeholders, it is time to execute the application, and eventhub_dashboardclick the button to send events. We should see the updates in Event hub dashboard as shown here. If not, check out the AsyncHttpClient callback handler and see the response from the server after the POST request.

Hope, many more Android apps will start publishing events to Azure Event hubs 🙂

7 thoughts on “Send events from Android app to Microsoft Azure Event Hubs”

  1. Very nice tutorial !
    Had a couple of questions – I am able to see “Successful” and “Failed Requests” in my Dashboard. However, I am not able to see any count for the “Incoming Messages” metric. With this solution, were you able to get any incoming messages? I was wondering how one can view the messages or events sent to the hub to get confirmation that the data is indeed being received.

    Also, for a single button click on the app, it seems like there are a number of requests generated.

    1. Hi KR7, Usually the dashboard takes a while to update. There are couple of options to see the events. Writing another listener / Connecting to stream analytics and persisting the output to storage

  2. Hi, I am following same procedure but getting Unauthorized. I think problem with SAS, For SAS I am using the link above mentioned itself. Can you please guide me.

  3. Nice stuff.. I have sent data to eventhubs from C# apps using AMQP and it allows one to add partitionkey and a few other properties, Is something similar available in HTTP Post scenarios?
    If possible would appreciate if you could show in button click handler code..Thanks

  4. Great tutorial – interesting that you chose to use HTTP POST rather than AMQP 1.0. I am looking to use AMQP 1.0 but have found Android support for AMQP 1.0 client libraries to be almost non-existent. Have you had any success using AMQP 1.0 from Android with Event Hub?

  5. Cool tutorial! Great point about the brokered connections too (which are not expensive anyway), but you’re right, pick the right tool for the right job. Any chance we can get a sample using AMQP?

Leave a Reply