我如何使用用户名和密码 paho 客户端连接 Java mqtt 客户端
How can i connect a Java mqtt client with username and password paho client
此代码在 Android Studio 中有效,但我如何使用用户名和密码登录?
有人可以帮帮我吗
package com.example.mqtt;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
public class MainActivity extends AppCompatActivity {
MqttAndroidClient client;
TextView subText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subText = (TextView)findViewById(R.id.subText);
///This here are the mqtt broker informations
String clientId = MqttClient.generateClientId();
client = new MqttAndroidClient(this.getApplicationContext(), "mqtt://0.0.2.2:1883",clientId);
try {
IMqttToken token = client.connect();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Toast.makeText(MainActivity.this,"connected!!",Toast.LENGTH_LONG).show();
setSubscription();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Toast.makeText(MainActivity.this,"connection failed!!",Toast.LENGTH_LONG).show();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
subText.setText(new String(message.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
}
public void published(View v){
String topic = "event";
String message = "the payload";
try {
client.publish(topic, message.getBytes(),0,false);
Toast.makeText(this,"Published Message",Toast.LENGTH_SHORT).show();
} catch ( MqttException e) {
e.printStackTrace();
}
}
private void setSubscription(){
try{
client.subscribe("event",0);
}catch (MqttException e){
e.printStackTrace();
}
}
public void conn(View v){
try {
IMqttToken token = client.connect();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Toast.makeText(MainActivity.this,"connected!!",Toast.LENGTH_LONG).show();
setSubscription();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Toast.makeText(MainActivity.this,"connection failed!!",Toast.LENGTH_LONG).show();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
public void disconn(View v){
try {
IMqttToken token = client.disconnect();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Toast.makeText(MainActivity.this,"Disconnected!!",Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Toast.makeText(MainActivity.this,"Could not diconnect!!",Toast.LENGTH_LONG).show();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
}
您需要在传递 MqttConnectOptions 对象时使用 client.connect();
的版本
查看文档 here
String clientId = MqttClient.generateClientId();
client = new MqttAndroidClient(this.getApplicationContext(), "mqtt://0.0.2.2:1883",clientId);
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName("foo");
options.setPassword("bar".toCharArray());
try {
IMqttToken token = client.connect(options);
...
此代码在 Android Studio 中有效,但我如何使用用户名和密码登录? 有人可以帮帮我吗
package com.example.mqtt;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.IMqttToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
public class MainActivity extends AppCompatActivity {
MqttAndroidClient client;
TextView subText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subText = (TextView)findViewById(R.id.subText);
///This here are the mqtt broker informations
String clientId = MqttClient.generateClientId();
client = new MqttAndroidClient(this.getApplicationContext(), "mqtt://0.0.2.2:1883",clientId);
try {
IMqttToken token = client.connect();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Toast.makeText(MainActivity.this,"connected!!",Toast.LENGTH_LONG).show();
setSubscription();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Toast.makeText(MainActivity.this,"connection failed!!",Toast.LENGTH_LONG).show();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
subText.setText(new String(message.getPayload()));
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
}
public void published(View v){
String topic = "event";
String message = "the payload";
try {
client.publish(topic, message.getBytes(),0,false);
Toast.makeText(this,"Published Message",Toast.LENGTH_SHORT).show();
} catch ( MqttException e) {
e.printStackTrace();
}
}
private void setSubscription(){
try{
client.subscribe("event",0);
}catch (MqttException e){
e.printStackTrace();
}
}
public void conn(View v){
try {
IMqttToken token = client.connect();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Toast.makeText(MainActivity.this,"connected!!",Toast.LENGTH_LONG).show();
setSubscription();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Toast.makeText(MainActivity.this,"connection failed!!",Toast.LENGTH_LONG).show();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
public void disconn(View v){
try {
IMqttToken token = client.disconnect();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Toast.makeText(MainActivity.this,"Disconnected!!",Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Toast.makeText(MainActivity.this,"Could not diconnect!!",Toast.LENGTH_LONG).show();
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
}
您需要在传递 MqttConnectOptions 对象时使用 client.connect();
的版本
查看文档 here
String clientId = MqttClient.generateClientId();
client = new MqttAndroidClient(this.getApplicationContext(), "mqtt://0.0.2.2:1883",clientId);
MqttConnectOptions options = new MqttConnectOptions();
options.setUserName("foo");
options.setPassword("bar".toCharArray());
try {
IMqttToken token = client.connect(options);
...