#gradle project sync failed in android studio
Explore tagged Tumblr posts
Text
Android Studio - Gradle project refresh/sync failed : connection timed out
Android Studio – Gradle project refresh/sync failed : connection timed out
This article tries to address following issues while starting Android Studio Gradle sync failed: Connection timed out Gradle project refresh failed : connection timed out If you are getting this error while starting Android studio, it will be mostly because its not able to connect to Internet. Check if you are able to connect to internet from Android Studio. For this, go to File -> Settings…
View On WordPress
#Android Studio - Gradle project refresh/sync failed#gradle project refresh failed in android studio#gradle project sync failed in android studio
0 notes
Text
How to import project in Android Studio?
Migration of your projects to Android Studio needs you to adapt to a new project structure, IDE functionality, and build system. Suppose you are migrating a project in Android Studio from Eclipse. In that case, Android Studio will offer you an import tool to move the existing code into the Android Studio project and Gradle-based build files. Before discussing further on how to import project in Android Studio? Let us understand what Android Studio is.
What is Android Studio?
The official Integrated development environment(IDE) for Android app development is Android Studio. It is based on Java integrated development environment for Software called IntelliJ IDEA. It includes code editing and developer tools. It utilizes code templates, GitHub integration, emulator, and support app development within the Android operating system.
So let’s learn here how to import a project in Android Studio. There are 4 steps mentioned here to place the project in Android Studio.
Open your Android Studio and if any project is opened just close it (Go to File?Close Project) then you'll see a small window like this:
Step: 1
To import a project in Android Studio, you should open Android Studio Project: Click on "Open an existing Android Studio project" to open the Android Studio Project.
Select your Android Studio Project directory from the 'Open file to Project' dialogue and click on the OK button.
Wait until the project sync and builds project in Android Studio.
Step:2
Open Gradle or Eclipse ADT project: Click on “Import Project(Gradle, Eclipse ADT etc)” to open Eclipse build project for placing the project in Android studio without fail.
Here, browse your project in Android Studio by navigating the location where you kept your project, your project’s folder will appear with an Android logo on it like this:
Select the app and click OK, this will take some time for the Gradle to build, wait for the loading time. (You might get an error here stating the different SDK location, the error looks like this):
In case of the above error just go to Project Directory and you’ll then find the file named “local.properties” in the root folder of your project. Open this file and scroll to the last 2 lines “ndk” and “SDK” and change the location with the one of your SDK and save the file.
Again open the Android Studio Project or else if already opened in Android studio, go to Gradle->Rebuild.
Step: 3
To build and run your app: In the toolbar, select your app from the run configurations drop-down menu.
From the target device drop-down menu, select the device you want to run your app.
Note: If you don't have any devices configured, then you need to either connect a device via USB or create an AVD to use the Android Emulator
Click on the Run button in Toolbar or Select the Run menu in the menubar
Step:4
Edit project :
By default, while you import project in Android Studio, the Android Studio displays your project files in the Android view. This view does not reflect the actual file hierarchy on disk, but is organized by modules and file types to simplify navigation between key source files of your Android Studio Project, hiding certain files or directories that are not commonly used. Some of the structural changes compared to the structure on disk include the following:
Shows all the project's build-related configuration files in a top-level Gradle Script group.
Shows all manifest files for each module in a module-level group(when you have different manifest files for different product flavors and build types).
Shows all alternative resource files in a single group, instead of in separate folders per resource qualifier. For example, all density versions of your launcher icon are visible side-by-side.
Within each Android app module, files are shown in the following groups:
manifests: Contains the AndroidManifest.xml file.
java: Contains the Java source code files, separated by package names, including JUnit test code.
res: Contains all non-code resources, such as XML layouts, UI strings, and bitmap images, divided into corresponding sub-directories. For more information about all possible resource types, see Providing Resources.
Gradle Scripts: Two types of Gradle file are used in android projects One type is Project level Gradle which contains application repositories, dependencies, and project-level variables, Second type are modules level Gradle which contain app version name, version code, min version, application ID, dependencies for this module, debug and release build types, build flavors, etc
Conclusion
It is a simple process to migrate your project to Android Studio. So follow these steps, which show you how to import projects in Android Studio.
#android studio#how to import project in android studio#Project in Android Studio#software engineering#webdev
1 note
·
View note
Text
Building an Event-Driven Kotlin Android App using MQTT and Solace PubSub+ Event Broker: Cloud
MQTT is a lightweight publish-subscribe messaging protocol that’s fast becoming the de facto protocol of choice for Internet of Things (IoT) applications. MQTT’s popularity is largely due to its small bandwidth usage and low power consumption. In this article, we will leverage the Solace PubSub+ Event Broker’s native MQTT capabilities to build a simple event-driven Android app that uses the publish-subscribe messaging pattern. We will be using Kotlin as our programming language of choice when writing the Android app.
Creating a Solace PubSub+ Cloud Messaging Service
To build an event-driven application, you need to connect it to an event broker so that it could send and receive events asynchronously. Use Solace PubSub+ Cloud to create a free Messaging Service. Once the messaging service is up and running, navigate to its Connect tab and note its MQTT Connection Details. These details will later be used to connect our app to the Solace PubSub+ Event Broker.
Building an Event-Driven Android App in Kotlin
We will use the Android Studio IDE to build a new Kotlin project. The project will use the Eclipse Paho MQTT client library as our MQTT client library. This MQTT client library will be wrapped with a helper class that only exposes the methods we’ll need to publish and subscribe to Solace events. After writing the helper class, we will then build a simple Android MainActivity that we can use to subscribe to a user-defined topic and publish messages to the same topic.
Setting up the Kotlin project using Android Studio
After downloading and installing Android Studio, create a new Kotlin project with an Empty Activity by navigating to File > New Project > Empty Activity and selecting Kotlin as the Language.
Configuring the Project and its Dependencies
Once the project is set up, configure the project’s Gradle dependencies by navigating to build.gradle (Project: My Application) and adding the following repository which contains the Eclipse MQTT Paho library:
repositories { // <other repositories that might already exist, we will leave as is> // paho repository maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" } }
Navigate to app/build.gradle and add the following dependencies to fetch the Eclipse MQTT Paho client library:
dependencies { // <other dependencies that might already exist, we will leave as is> // paho dependencies implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' }
At this point Android Studio should prompt you to sync these Gradle dependencies to your project. Click on Sync Now to do so.
Next, configure the project’s AndroidManifest.xml file to give the app the required access permissions as well as register the MQTT client library as an Android service:
<manifest ...> // <other user permissions that might already exist, we will leave as is> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application ... // <other configuration that might already exist, we will leave as is> <service android:name="org.eclipse.paho.android.service.MqttService" /> </application> </manifest>
Configuring Broker Connection Details
Once the Android Studio project is set up, create a new Kotlin file – MessagingOptions.kt – that will contain the broker connection details that the app will use to connect to the Solace PubSub+ Event Broker (as noted from the first step):
package com.example.myapplication // Solace PubSub+ Broker Options // Fill in your Solace Cloud PubSub+ Broker's 'MQTT Host' and 'Password' options. // This information can be found under: // https://console.solace.cloud/services/ -> <your-service> -> 'Connect' -> 'MQTT' const val SOLACE_CLIENT_USER_NAME = "solace-cloud-client" const val SOLACE_CLIENT_PASSWORD = "<your-service-password>" const val SOLACE_MQTT_HOST = "tcp://<your-service-mqtt-host>.messaging.solace.cloud:1883" // Other options const val SOLACE_CONNECTION_TIMEOUT = 3 const val SOLACE_CONNECTION_KEEP_ALIVE_INTERVAL = 60 const val SOLACE_CONNECTION_CLEAN_SESSION = true const val SOLACE_CONNECTION_RECONNECT = true
Adding the MQTT helper class
Next, create a new MQTT package and add a helper class to it: MqttClientHelper. This helper class will wrap the MQTT Client library methods that the app will use. The helper class will make use of the MQTT connection options that were previously specified:
package com.example.myapplication.mqtt import android.content.Context import android.util.Log import com.example.myapplication.* import org.eclipse.paho.android.service.MqttAndroidClient import org.eclipse.paho.client.mqttv3.* import org.eclipse.paho.client.mqttv3.MqttClient class MqttClientHelper(context: Context?) { companion object { const val TAG = "MqttClientHelper" } var mqttAndroidClient: MqttAndroidClient val serverUri = SOLACE_MQTT_HOST private val clientId: String = MqttClient.generateClientId() fun setCallback(callback: MqttCallbackExtended?) { mqttAndroidClient.setCallback(callback) } init { mqttAndroidClient = MqttAndroidClient(context, serverUri, clientId) mqttAndroidClient.setCallback(object : MqttCallbackExtended { override fun connectComplete(b: Boolean, s: String) { Log.w(TAG, s) } override fun connectionLost(throwable: Throwable) {} @Throws(Exception::class) override fun messageArrived( topic: String, mqttMessage: MqttMessage ) { Log.w(TAG, mqttMessage.toString()) } override fun deliveryComplete(iMqttDeliveryToken: IMqttDeliveryToken) {} }) connect() } private fun connect() { val mqttConnectOptions = MqttConnectOptions() mqttConnectOptions.isAutomaticReconnect = SOLACE_CONNECTION_RECONNECT mqttConnectOptions.isCleanSession = SOLACE_CONNECTION_CLEAN_SESSION mqttConnectOptions.userName = SOLACE_CLIENT_USER_NAME mqttConnectOptions.password = SOLACE_CLIENT_PASSWORD.toCharArray() mqttConnectOptions.connectionTimeout = SOLACE_CONNECTION_TIMEOUT mqttConnectOptions.keepAliveInterval = SOLACE_CONNECTION_KEEP_ALIVE_INTERVAL try { mqttAndroidClient.connect(mqttConnectOptions, null, object : IMqttActionListener { override fun onSuccess(asyncActionToken: IMqttToken) { val disconnectedBufferOptions = DisconnectedBufferOptions() disconnectedBufferOptions.isBufferEnabled = true disconnectedBufferOptions.bufferSize = 100 disconnectedBufferOptions.isPersistBuffer = false disconnectedBufferOptions.isDeleteOldestMessages = false mqttAndroidClient.setBufferOpts(disconnectedBufferOptions) } override fun onFailure( asyncActionToken: IMqttToken, exception: Throwable ) { Log.w(TAG, "Failed to connect to: $serverUri ; $exception") } }) } catch (ex: MqttException) { ex.printStackTrace() } } fun subscribe(subscriptionTopic: String, qos: Int = 0) { try { mqttAndroidClient.subscribe(subscriptionTopic, qos, null, object : IMqttActionListener { override fun onSuccess(asyncActionToken: IMqttToken) { Log.w(TAG, "Subscribed to topic '$subscriptionTopic'") } override fun onFailure( asyncActionToken: IMqttToken, exception: Throwable ) { Log.w(TAG, "Subscription to topic '$subscriptionTopic' failed!") } }) } catch (ex: MqttException) { System.err.println("Exception whilst subscribing to topic '$subscriptionTopic'") ex.printStackTrace() } } fun publish(topic: String, msg: String, qos: Int = 0) { try { val message = MqttMessage() message.payload = msg.toByteArray() mqttAndroidClient.publish(topic, message.payload, qos, false) Log.d(TAG, "Message published to topic `$topic`: $msg") } catch (e: MqttException) { Log.d(TAG, "Error Publishing to $topic: " + e.message) e.printStackTrace() } } fun isConnected() : Boolean { return mqttAndroidClient.isConnected } fun destroy() { mqttAndroidClient.unregisterResources() mqttAndroidClient.disconnect() } }
Building the App’s Main Activity
At this point, you should have the setup needed to build your own event-driven Android application in whatever way you would like. What follows is an example implementation of a simple Android app that uses a publish-subscribe messaging pattern. The app has three input fields: the topic to subscribe to, the topic to publish to, and the message payload to publish. If subscribed to a topic (e.g. my/topic), you may publish a message to the same topic with a Message Payload of your choosing. By doing so, the app would be publishing the message with the specified message payload to the Solace PubSub+ Event Broker. The broker would then send that message to its subscribers (in this case, the app itself), and the app’s messageArrived() callback would then be triggered to update its Received Message Payloads view.
To build the app above, you will need to copy the following files to your project and rebuild it:
app/src/main/java/com/example/myapplication/MainActivity.kt
app/src/main/res/layout/content_main.xml
app/src/main/res/values/strings.xml
When the app is launched, its MainActivity invokes the MqttClientHelper which in turn attempts to connect to the Solace PubSub+ Event Broker as per its init implementation. The connection details that the init logic uses are described in MessagingOptions.kt. You can verify that the app connected to your Solace PubSub+ Event Broker successfully by navigating to Messaging Services > your-service-name > Manage Service > Client Connections > MQTT &> MQTT Clients in the Solace Cloud Console. You should then see one Paho MQTT client listed under the MQTT Clients tab.
Subscribing to Topics from Other Brokers
The app above while very simplistic in its implementation, could also be used to retrieve events from other applications that are already publishing data to other event brokers.
As part of the covid19-stream-processors project, Solace has made-available topics related to the COVID-19 Coronavirus – aggregated from different sources – as data that’s easily consumable by other event-driven applications. This data is being published – by an event-driven application – to a Solace PubSub+ Event Broker that uses dynamic topics. These dynamic topics allow subscribers to pick, choose, and filter on the specific data that they want to consume.
We will use our Android app, as-is, to subscribe to one of the topics from the event broker that’s serving the covid19-stream-processors data.
Modifying Broker Connection Details
To connect the the covid19-stream-processors event broker, modify the MessagingOptions.kt file as per the connection information from the covid19-stream-processors GitHub page:
const val SOLACE_CLIENT_USER_NAME = "covid-public-client" const val SOLACE_CLIENT_PASSWORD = "covid19" const val SOLACE_MQTT_HOST = "tcp://mr2r9za6fwi0wf.messaging.solace.cloud:1883"
Subscribing to a Topic from an External Broker
After updating MessagingOptions.kt, launch the app and it should notify you that it has connected to the covid19-stream-processors Event Broker (host) at the bottom. Once the app connects successfully, you can then subscribe to one of the topics outlined in the project (e.g. jhu/csse/covid19/raw) by typing it into the input field and selecting SUBSCRIBE. After ~45 seconds, you should begin to see JSON message payloads being received by the Android app.
Conclusion
In this article, we showcased how to create an event-driven Android app with a Solace PubSub+ Cloud Messaging Service as its event broker. The app was implemented in Kotlin using a publish-subscribe messaging pattern to connect to the Solace PubSub+ Event Broker. The established connection used the MQTT messaging protocol as its messaging transport to demonstrate the publish-subscribe model. The app was then used to connect to an external pre-configured event broker to consume public data related to COVID-19 that’s being published by an external application.
The full implementation of the Android app discussed in this article can be found in this GitHub repository.
The post Building an Event-Driven Kotlin Android App using MQTT and Solace PubSub+ Event Broker: Cloud appeared first on Solace.
Building an Event-Driven Kotlin Android App using MQTT and Solace PubSub+ Event Broker: Cloud published first on https://jiohow.tumblr.com/
0 notes
Video
youtube
How To Fix Gradle Project Sync Failed Android Studio ✔️
Android Studio Tutorial: Fix gradle project sync failed android studio when you import project in android studio. Read more here: http://bit.ly/raqmedia
0 notes
Text
How to clone any website||How to copy all webpage of a website httrack
https://opix.pk/blog/how-to-clone-any-websitehow-to-copy-all-webpage-of-a-website-httrack/ How to clone any website||How to copy all webpage of a website httrack https://opix.pk/blog/how-to-clone-any-websitehow-to-copy-all-webpage-of-a-website-httrack/ Opix.pk #all #any #blogsites #cloner #copy #datingsites #download #duplicate #freedatingsites #freewebsite #get #hack #how #howtocloneawebsite #howtocloneanywebsite #howtocopyawebsite #howtocopyanywebsite/cloneallwebpages #howtohack #howtoinstallsdkinandroidstudio #howto... #layout #make #replicate #site #sites #vevo #web #webdesign #Webdeveloper #webhistory #website #websites #webdesign #website #websitecreator #websitedesign #websitedesigner #websitetemplates How to make your first website without any skill , follow this video.https://youtu.be/RAIfChai-Uo Requirement A system with internet any browser (I use crome) Httrack software Notepad++ Littile knowledge about html/css create website without coding, create html website without coding, create wordpress website without coding, how to copy any website, how to copy any website template, how to clone a website, how to make clone of website, revampy review, revampy demo, how to clone html site, how to clone website site, clone website, how to copy website, lentech -~-~~-~~~-~~-~- Please watch: “How To Fix Gradle Project Sync Failed In Android Studio” -~-~~-~~~-~~-~- How To Connect MySQL With PHP in Your LocalServer / XAMPP Create A New Database How To Make Free Website for a Beginner Step By Step Guide : Free Domain , Free Hosting Tutorial How To Convert A PSD File To A Live Website Parrt 1source
0 notes
Text
Building an Event-Driven Kotlin Android App using MQTT and Solace PubSub+ Event Broker: Cloud
MQTT is a lightweight publish-subscribe messaging protocol that’s fast becoming the de facto protocol of choice for Internet of Things (IoT) applications. MQTT’s popularity is largely due to its small bandwidth usage and low power consumption. In this article, we will leverage the Solace PubSub+ Event Broker’s native MQTT capabilities to build a simple event-driven Android app that uses the publish-subscribe messaging pattern. We will be using Kotlin as our programming language of choice when writing the Android app.
Creating a Solace PubSub+ Cloud Messaging Service
To build an event-driven application, you need to connect it to an event broker so that it could send and receive events asynchronously. Use Solace PubSub+ Cloud to create a free Messaging Service. Once the messaging service is up and running, navigate to its Connect tab and note its MQTT Connection Details. These details will later be used to connect our app to the Solace PubSub+ Event Broker.
Building an Event-Driven Android App in Kotlin
We will use the Android Studio IDE to build a new Kotlin project. The project will use the Eclipse Paho MQTT client library as our MQTT client library. This MQTT client library will be wrapped with a helper class that only exposes the methods we’ll need to publish and subscribe to Solace events. After writing the helper class, we will then build a simple Android MainActivity that we can use to subscribe to a user-defined topic and publish messages to the same topic.
Setting up the Kotlin project using Android Studio
After downloading and installing Android Studio, create a new Kotlin project with an Empty Activity by navigating to File > New Project > Empty Activity and selecting Kotlin as the Language.
Configuring the Project and its Dependencies
Once the project is set up, configure the project’s Gradle dependencies by navigating to build.gradle (Project: My Application) and adding the following repository which contains the Eclipse MQTT Paho library:
repositories { // <other repositories that might already exist, we will leave as is> // paho repository maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" } }
Navigate to app/build.gradle and add the following dependencies to fetch the Eclipse MQTT Paho client library:
dependencies { // <other dependencies that might already exist, we will leave as is> // paho dependencies implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0' implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1' }
At this point Android Studio should prompt you to sync these Gradle dependencies to your project. Click on Sync Now to do so.
Next, configure the project’s AndroidManifest.xml file to give the app the required access permissions as well as register the MQTT client library as an Android service:
<manifest ...> // <other user permissions that might already exist, we will leave as is> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application ... // <other configuration that might already exist, we will leave as is> <service android:name="org.eclipse.paho.android.service.MqttService" /> </application> </manifest>
Configuring Broker Connection Details
Once the Android Studio project is set up, create a new Kotlin file – MessagingOptions.kt – that will contain the broker connection details that the app will use to connect to the Solace PubSub+ Event Broker (as noted from the first step):
package com.example.myapplication // Solace PubSub+ Broker Options // Fill in your Solace Cloud PubSub+ Broker's 'MQTT Host' and 'Password' options. // This information can be found under: // https://console.solace.cloud/services/ -> <your-service> -> 'Connect' -> 'MQTT' const val SOLACE_CLIENT_USER_NAME = "solace-cloud-client" const val SOLACE_CLIENT_PASSWORD = "<your-service-password>" const val SOLACE_MQTT_HOST = "tcp://<your-service-mqtt-host>.messaging.solace.cloud:1883" // Other options const val SOLACE_CONNECTION_TIMEOUT = 3 const val SOLACE_CONNECTION_KEEP_ALIVE_INTERVAL = 60 const val SOLACE_CONNECTION_CLEAN_SESSION = true const val SOLACE_CONNECTION_RECONNECT = true
Adding the MQTT helper class
Next, create a new MQTT package and add a helper class to it: MqttClientHelper. This helper class will wrap the MQTT Client library methods that the app will use. The helper class will make use of the MQTT connection options that were previously specified:
package com.example.myapplication.mqtt import android.content.Context import android.util.Log import com.example.myapplication.* import org.eclipse.paho.android.service.MqttAndroidClient import org.eclipse.paho.client.mqttv3.* import org.eclipse.paho.client.mqttv3.MqttClient class MqttClientHelper(context: Context?) { companion object { const val TAG = "MqttClientHelper" } var mqttAndroidClient: MqttAndroidClient val serverUri = SOLACE_MQTT_HOST private val clientId: String = MqttClient.generateClientId() fun setCallback(callback: MqttCallbackExtended?) { mqttAndroidClient.setCallback(callback) } init { mqttAndroidClient = MqttAndroidClient(context, serverUri, clientId) mqttAndroidClient.setCallback(object : MqttCallbackExtended { override fun connectComplete(b: Boolean, s: String) { Log.w(TAG, s) } override fun connectionLost(throwable: Throwable) {} @Throws(Exception::class) override fun messageArrived( topic: String, mqttMessage: MqttMessage ) { Log.w(TAG, mqttMessage.toString()) } override fun deliveryComplete(iMqttDeliveryToken: IMqttDeliveryToken) {} }) connect() } private fun connect() { val mqttConnectOptions = MqttConnectOptions() mqttConnectOptions.isAutomaticReconnect = SOLACE_CONNECTION_RECONNECT mqttConnectOptions.isCleanSession = SOLACE_CONNECTION_CLEAN_SESSION mqttConnectOptions.userName = SOLACE_CLIENT_USER_NAME mqttConnectOptions.password = SOLACE_CLIENT_PASSWORD.toCharArray() mqttConnectOptions.connectionTimeout = SOLACE_CONNECTION_TIMEOUT mqttConnectOptions.keepAliveInterval = SOLACE_CONNECTION_KEEP_ALIVE_INTERVAL try { mqttAndroidClient.connect(mqttConnectOptions, null, object : IMqttActionListener { override fun onSuccess(asyncActionToken: IMqttToken) { val disconnectedBufferOptions = DisconnectedBufferOptions() disconnectedBufferOptions.isBufferEnabled = true disconnectedBufferOptions.bufferSize = 100 disconnectedBufferOptions.isPersistBuffer = false disconnectedBufferOptions.isDeleteOldestMessages = false mqttAndroidClient.setBufferOpts(disconnectedBufferOptions) } override fun onFailure( asyncActionToken: IMqttToken, exception: Throwable ) { Log.w(TAG, "Failed to connect to: $serverUri ; $exception") } }) } catch (ex: MqttException) { ex.printStackTrace() } } fun subscribe(subscriptionTopic: String, qos: Int = 0) { try { mqttAndroidClient.subscribe(subscriptionTopic, qos, null, object : IMqttActionListener { override fun onSuccess(asyncActionToken: IMqttToken) { Log.w(TAG, "Subscribed to topic '$subscriptionTopic'") } override fun onFailure( asyncActionToken: IMqttToken, exception: Throwable ) { Log.w(TAG, "Subscription to topic '$subscriptionTopic' failed!") } }) } catch (ex: MqttException) { System.err.println("Exception whilst subscribing to topic '$subscriptionTopic'") ex.printStackTrace() } } fun publish(topic: String, msg: String, qos: Int = 0) { try { val message = MqttMessage() message.payload = msg.toByteArray() mqttAndroidClient.publish(topic, message.payload, qos, false) Log.d(TAG, "Message published to topic `$topic`: $msg") } catch (e: MqttException) { Log.d(TAG, "Error Publishing to $topic: " + e.message) e.printStackTrace() } } fun isConnected() : Boolean { return mqttAndroidClient.isConnected } fun destroy() { mqttAndroidClient.unregisterResources() mqttAndroidClient.disconnect() } }
Building the App’s Main Activity
At this point, you should have the setup needed to build your own event-driven Android application in whatever way you would like. What follows is an example implementation of a simple Android app that uses a publish-subscribe messaging pattern. The app has three input fields: the topic to subscribe to, the topic to publish to, and the message payload to publish. If subscribed to a topic (e.g. my/topic), you may publish a message to the same topic with a Message Payload of your choosing. By doing so, the app would be publishing the message with the specified message payload to the Solace PubSub+ Event Broker. The broker would then send that message to its subscribers (in this case, the app itself), and the app’s messageArrived() callback would then be triggered to update its Received Message Payloads view.
To build the app above, you will need to copy the following files to your project and rebuild it:
app/src/main/java/com/example/myapplication/MainActivity.kt
app/src/main/res/layout/content_main.xml
app/src/main/res/values/strings.xml
When the app is launched, its MainActivity invokes the MqttClientHelper which in turn attempts to connect to the Solace PubSub+ Event Broker as per its init implementation. The connection details that the init logic uses are described in MessagingOptions.kt. You can verify that the app connected to your Solace PubSub+ Event Broker successfully by navigating to Messaging Services > your-service-name > Manage Service > Client Connections > MQTT &> MQTT Clients in the Solace Cloud Console. You should then see one Paho MQTT client listed under the MQTT Clients tab.
Subscribing to Topics from Other Brokers
The app above while very simplistic in its implementation, could also be used to retrieve events from other applications that are already publishing data to other event brokers.
As part of the covid19-stream-processors project, Solace has made-available topics related to the COVID-19 Coronavirus – aggregated from different sources – as data that’s easily consumable by other event-driven applications. This data is being published – by an event-driven application – to a Solace PubSub+ Event Broker that uses dynamic topics. These dynamic topics allow subscribers to pick, choose, and filter on the specific data that they want to consume.
We will use our Android app, as-is, to subscribe to one of the topics from the event broker that’s serving the covid19-stream-processors data.
Modifying Broker Connection Details
To connect the the covid19-stream-processors event broker, modify the MessagingOptions.kt file as per the connection information from the covid19-stream-processors GitHub page:
const val SOLACE_CLIENT_USER_NAME = "covid-public-client" const val SOLACE_CLIENT_PASSWORD = "covid19" const val SOLACE_MQTT_HOST = "tcp://mr2r9za6fwi0wf.messaging.solace.cloud:1883"
Subscribing to a Topic from an External Broker
After updating MessagingOptions.kt, launch the app and it should notify you that it has connected to the covid19-stream-processors Event Broker (host) at the bottom. Once the app connects successfully, you can then subscribe to one of the topics outlined in the project (e.g. jhu/csse/covid19/raw) by typing it into the input field and selecting SUBSCRIBE. After ~45 seconds, you should begin to see JSON message payloads being received by the Android app.
Conclusion
In this article, we showcased how to create an event-driven Android app with a Solace PubSub+ Cloud Messaging Service as its event broker. The app was implemented in Kotlin using a publish-subscribe messaging pattern to connect to the Solace PubSub+ Event Broker. The established connection used the MQTT messaging protocol as its messaging transport to demonstrate the publish-subscribe model. The app was then used to connect to an external pre-configured event broker to consume public data related to COVID-19 that’s being published by an external application.
The full implementation of the Android app discussed in this article can be found in this GitHub repository.
The post Building an Event-Driven Kotlin Android App using MQTT and Solace PubSub+ Event Broker: Cloud appeared first on Solace.
Building an Event-Driven Kotlin Android App using MQTT and Solace PubSub+ Event Broker: Cloud published first on https://jiohow.tumblr.com/
0 notes