安卓 – MQTT 和 Mosquitto

huangapple go评论108阅读模式
英文:

Android - MQTT and Mosquitto

问题

I'm working in a project that trying to publish and subscribe to the MQTTHOST test.mosquitto.org:1833
But when I run my code, always occurs "your app keeps stopping".

I used the same code from the YouTube video (Paho): https://www.youtube.com/watch?v=BAkGm02WBc0

My code:

MainActivity

  1. package com.carlospacheco.mce_app_ifsp;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Toast;
  6. import org.eclipse.paho.android.service.MqttAndroidClient;
  7. import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  8. import org.eclipse.paho.client.mqttv3.IMqttToken;
  9. import org.eclipse.paho.client.mqttv3.MqttClient;
  10. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  11. import org.eclipse.paho.client.mqttv3.MqttException;
  12. public class MainActivity extends AppCompatActivity {
  13. static String MQTTHOST = "test.mosquitto.org:1883";
  14. static String USERNAME = "";
  15. static String PASSWORD = "";
  16. String topicStr = "LED";
  17. MqttAndroidClient client;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. String clientId = MqttClient.generateClientId();
  23. client = new MqttAndroidClient(this.getApplicationContext(), MQTTHOST, clientId);
  24. MqttConnectOptions options = new MqttConnectOptions();
  25. options.setUserName(USERNAME);
  26. options.setPassword(PASSWORD.toCharArray());
  27. try {
  28. IMqttToken token = client.connect(options);
  29. token.setActionCallback(new IMqttActionListener() {
  30. @Override
  31. public void onSuccess(IMqttToken asyncActionToken) {
  32. Toast.makeText(MainActivity.this, "Conectado!", Toast.LENGTH_LONG).show();
  33. }
  34. @Override
  35. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  36. Toast.makeText(MainActivity.this, "Conexão Falhou!", Toast.LENGTH_LONG).show();
  37. }
  38. });
  39. } catch (MqttException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. public void pub(View v) {
  44. String topic = topicStr;
  45. String message = "hello world from ifi gaming";
  46. try {
  47. client.publish(topic, message.getBytes(), 0, false);
  48. } catch (MqttException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }

build.gradle

  1. apply plugin: 'com.android.application'
  2. android {
  3. compileSdkVersion 29
  4. buildToolsVersion "29.0.3"
  5. defaultConfig {
  6. applicationId "com.carlospacheco.mce_app_ifsp"
  7. minSdkVersion 16
  8. targetSdkVersion 29
  9. versionCode 1
  10. versionName "1.0"
  11. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  12. }
  13. buildTypes {
  14. release {
  15. minifyEnabled false
  16. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  17. }
  18. }
  19. }
  20. repositories {
  21. maven {
  22. url "https://repo.eclipse.org/content/repositories/paho-releases/"
  23. }
  24. }
  25. dependencies {
  26. implementation fileTree(dir: 'libs', include: ['*.jar'])
  27. implementation 'androidx.appcompat:appcompat:1.2.0'
  28. implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  29. implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
  30. testImplementation 'junit:junit:4.12'
  31. androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  32. androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  33. compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
  34. exclude module: 'support-v4'
  35. }
  36. }

activity main

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <Button
  9. android:id="@+id/button"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="Conectar"
  13. android:onClick="pub"
  14. app:layout_constraintEnd_toEndOf="parent"
  15. app:layout_constraintStart_toStartOf="parent"
  16. tools:layout_editor_absoluteY="45dp" />
  17. </androidx.constraintlayout.widget.ConstraintLayout>

安卓 – MQTT 和 Mosquitto

英文:

I'm working in a project that trying to publish and subscribe to the MQTTHOST test.mosquitto.org:1833
But when I run my code, always occurrs "you app keeps stopping".

I used the same code from the youtube video, (Paho): https://www.youtube.com/watch?v=BAkGm02WBc0

My code:

MainActivity

  1. package com.carlospacheco.mce_app_ifsp;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Toast;
  6. import org.eclipse.paho.android.service.MqttAndroidClient;
  7. import org.eclipse.paho.client.mqttv3.IMqttActionListener;
  8. import org.eclipse.paho.client.mqttv3.IMqttToken;
  9. import org.eclipse.paho.client.mqttv3.MqttClient;
  10. import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
  11. import org.eclipse.paho.client.mqttv3.MqttException;
  12. public class MainActivity extends AppCompatActivity {
  13. static String MQTTHOST = &quot;test.mosquitto.org:1883&quot;;
  14. static String USERNAME = &quot;&quot;;
  15. static String PASSWORD = &quot;&quot;;
  16. String topicStr = &quot;LED&quot;;
  17. MqttAndroidClient client;
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. String clientId = MqttClient.generateClientId();
  23. client = new MqttAndroidClient(this.getApplicationContext(), MQTTHOST, clientId);
  24. MqttConnectOptions options = new MqttConnectOptions();
  25. options.setUserName(USERNAME);
  26. options.setPassword(PASSWORD.toCharArray());
  27. try {
  28. IMqttToken token = client.connect(options);
  29. token.setActionCallback(new IMqttActionListener() {
  30. @Override
  31. public void onSuccess(IMqttToken asyncActionToken) {
  32. Toast.makeText(MainActivity.this,&quot;Conectado!&quot;, Toast.LENGTH_LONG).show();
  33. }
  34. @Override
  35. public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
  36. Toast.makeText(MainActivity.this,&quot;Conex&#227;o Falhou!&quot;, Toast.LENGTH_LONG).show();
  37. }
  38. });
  39. } catch (MqttException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. public void pub(View v){
  44. String topic = topicStr;
  45. String message = &quot;hello world from ifi gaming&quot;;
  46. try {
  47. client.publish(topic, message.getBytes(),0, false);
  48. } catch (MqttException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }

build.gradle

  1. apply plugin: &#39;com.android.application&#39;
  2. android {
  3. compileSdkVersion 29
  4. buildToolsVersion &quot;29.0.3&quot;
  5. defaultConfig {
  6. applicationId &quot;com.carlospacheco.mce_app_ifsp&quot;
  7. minSdkVersion 16
  8. targetSdkVersion 29
  9. versionCode 1
  10. versionName &quot;1.0&quot;
  11. testInstrumentationRunner &quot;androidx.test.runner.AndroidJUnitRunner&quot;
  12. }
  13. buildTypes {
  14. release {
  15. minifyEnabled false
  16. proguardFiles getDefaultProguardFile(&#39;proguard-android-optimize.txt&#39;), &#39;proguard-rules.pro&#39;
  17. }
  18. }
  19. }
  20. repositories {
  21. maven {
  22. url &quot;https://repo.eclipse.org/content/repositories/paho-releases/&quot;
  23. }
  24. }
  25. dependencies {
  26. implementation fileTree(dir: &#39;libs&#39;, include: [&#39;*.jar&#39;])
  27. implementation &#39;androidx.appcompat:appcompat:1.2.0&#39;
  28. implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
  29. implementation &#39;androidx.localbroadcastmanager:localbroadcastmanager:1.0.0&#39;
  30. testImplementation &#39;junit:junit:4.12&#39;
  31. androidTestImplementation &#39;androidx.test.ext:junit:1.1.1&#39;
  32. androidTestImplementation &#39;androidx.test.espresso:espresso-core:3.2.0&#39;
  33. compile(&#39;org.eclipse.paho:org.eclipse.paho.android.service:1.0.2&#39;) {
  34. exclude module: &#39;support-v4&#39;
  35. }
  36. }

activity main

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;match_parent&quot;
  7. tools:context=&quot;.MainActivity&quot;&gt;
  8. &lt;Button
  9. android:id=&quot;@+id/button&quot;
  10. android:layout_width=&quot;wrap_content&quot;
  11. android:layout_height=&quot;wrap_content&quot;
  12. android:text=&quot;Conectar&quot;
  13. android:onClick=&quot;pub&quot;
  14. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  15. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  16. tools:layout_editor_absoluteY=&quot;45dp&quot; /&gt;
  17. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

安卓 – MQTT 和 Mosquitto

答案1

得分: 1

正如我在评论中所说,如果没有完整的异常堆栈跟踪,很难正确地回答这个问题,但我会猜一下。

变量 MQTTHOST 的值应该是一个指向 MQTT 代理的完整 URI。目前你使用的是 test.mosquitto.org:1883,这并不是一个完整的 URI。

一个完整的 URI 需要在主机部分之前包含一个协议。

在这种情况下,你需要在主机和端口组件之前添加 tcp://mqtt://

  1. static String MQTTHOST = "tcp://test.mosquitto.org:1883";
英文:

As I said in the comments, without the full exception stack trace this question is impossible to answer properly but I'll take a guess

The value of MQTTHOST is supposed to be a full URI pointing at the MQTT broker. At the moment you have test.mosquitto.org:1883 which is not a full URI.

A full URI needs to include a protocol before the host section.

In this case you need to add either tcp:// or mqtt:// before the host and port components.

  1. static String MQTTHOST = &quot;tcp://test.mosquitto.org:1883&quot;;

huangapple
  • 本文由 发表于 2020年8月20日 11:35:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63497915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定