Android Studio MqttAndroidClient.connect()到树莓派

gj3fmq9x  于 2023-01-05  发布在  Android
关注(0)|答案(1)|浏览(132)

我尝试在android studio中构建一个应用程序,使mqtt发布到Raspberry Pi上的代理。这是我的主布局的onCreate函数,但它返回此错误,我不知道为什么。我可以从命令提示符发布,所以代理没有问题。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bureau_klein);

    final String clientId = "Android1";
    final String ip = "192.168.1.198:1883";
    final MqttAndroidClient client = new MqttAndroidClient(this.getApplicationContext(), ip, clientId);

    try {
        IMqttToken token = client.connect();
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                Log.d("TAG", "onSuccess");
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
                Log.d("TAG", "onFailure "+ clientId + " " + ip);
                Log.e("MYAPP", "exception", exception);
                Log.e("MYAPP", "exception", asyncActionToken.getException());
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }

错误:

06-14 20:24:36.825 5891-5891/com.david.domotica2 E/MYAPP: exception
java.lang.IllegalArgumentException: 192.168.1.198:1883
  at org.eclipse.paho.client.mqttv3.MqttConnectOptions.validateURI(MqttConnectOptions.java:473)
  at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:273)
  at org.eclipse.paho.android.service.MqttConnection.connect(MqttConnection.java:282)
  at org.eclipse.paho.android.service.MqttService.connect(MqttService.java:323)
  at org.eclipse.paho.android.service.MqttAndroidClient.doConnect(MqttAndroidClient.java:462)
  at org.eclipse.paho.android.service.MqttAndroidClient.access$200(MqttAndroidClient.java:70)
  at org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection.onServiceConnected(MqttAndroidClient.java:109)
  at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1685)
  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1714)
  at android.os.Handler.handleCallback(Handler.java:789)
  at android.os.Handler.dispatchMessage(Handler.java:98)
  at android.os.Looper.loop(Looper.java:164)
  at android.app.ActivityThread.main(ActivityThread.java:6798)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
06-14 20:24:36.825 5891-5891/com.david.domotica2 E/MYAPP: exception
relj7zay

relj7zay1#

您传递给MqttAndroidClient的IP地址应该是完整的URI。
例如,您正在传入192.168.1.198:1883,它是IP地址和端口,但缺少架构(协议)
在这种情况下,应该传入tcp://192.168.1.198:1883

相关问题