gpt4 book ai didi

android - MqttAndroidClient - onFailure

转载 作者:行者123 更新时间:2023-11-29 02:40:09 26 4
gpt4 key购买 nike

问题

为什么我可以通过我的 mainActivity 连接到我的服务器,而不是通过 fragment ?

设置

程序 1 - 有效...

现在,我有两个程序。第一个称为 mqtt_test,它包括一个 MainActivity、一个 Button 和连接类 Mqtt。所以没有 fragment 。

通过该程序,我可以连接到我的服务器、订阅和接收消息。

第一个程序代码

public class mqtt {
private Context context;
private String broker = "tcp://10.34.5.134:1883";
private String clientId= "DefaultName";
private MqttAndroidClient client;
private ConnectionListener connectionListener;

private static final String TAG_MQTT= "MQTT";

public mqtt(Context context)
{
this.context = context;
}
public void connect()
{
final String clientId = MqttClient.generateClientId();

//set options
MqttConnectOptions options = new MqttConnectOptions();
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);

client =
new MqttAndroidClient(context, broker,
clientId);

try {
IMqttToken token = client.connect(options);
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// We are connected
Log.d(TAG_MQTT, "onSuccess");
connectionListener.connected(true);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
Log.d(TAG_MQTT, "connectionLost: ");
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.d(TAG_MQTT, "messageArrived: Message: " + message.toString() + " with topic: " + topic);
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.d(TAG_MQTT, "deliveryComplete: ");
}
});
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
// Something went wrong e.g. connection timeout or firewall problems
Log.d(TAG_MQTT, "onFailure");
connectionListener.connected(false);

}
});
} catch (MqttException e) {
e.printStackTrace();
}
}

public void subscribe(final String topic, int qos)
{
if(client.isConnected())
{
try {
IMqttToken subToken = client.subscribe(topic, qos);
subToken.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// The message was published
Log.d(TAG_MQTT, "onSuccess: Subscripted with topic: " + topic);
}

@Override
public void onFailure(IMqttToken asyncActionToken,
Throwable exception) {
// The subscription could not be performed, maybe the user was not
// authorized to subscribe on the specified topic e.g. using wildcards
Log.d(TAG_MQTT, "onFailure: The subscription failed");
}
});
} catch (MqttException e) {
e.printStackTrace();
}
}
}
public void publish(String topic, String payload, int qos)
{

byte[] encodedPayload = new byte[0];
try {
encodedPayload = payload.getBytes("UTF-8");
MqttMessage message = new MqttMessage(encodedPayload);
client.publish(topic, message);
Log.d(TAG_MQTT, "publish: Published message:" + payload + " to topic: " + topic);
} catch (UnsupportedEncodingException | MqttException e) {
e.printStackTrace();
Log.d(TAG_MQTT, "publish: Error");
}
}

public void setConnectionListener(ConnectionListener connectionListener) {
this.connectionListener = connectionListener;
}
}

程序 2 - 不起作用

在我的第二个程序中,我使用了几乎相同的代码。但是,我正在将 fragment 用于 future 的业务。在一些 fragment 中是按钮,它们正在调用正确的方法。如果我按下按钮,就会调用正确的方法,就像在第一个程序中一样。但是程序会跳过异步任务。永远不会调用 OnSucces 或 onFailure。

IMqttToken token = client.connect(null, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// connected
Log.d(TAG_CONNECTION, "onSuccess");
connectionListener.connected(true);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
Log.d(TAG_CONNECTION, "connectionLost: ");
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
Log.d(TAG_CONNECTION, "messageArrived: Message: " + message.toString() + " with topic: " + topic);
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.d(TAG_CONNECTION, "deliveryComplete: ");
}
});
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.d(TAG_CONNECTION, "onFailure");
connectionListener.connected(false);
Log.d(TAG_CONNECTION, exception.getMessage());

}
});

然后我在一个新线程上调用了连接方法:

@Override
public void doSmth() {
new Thread(new Runnable() {
public void run() {
// a potentially time consuming task
mqttConnection.connect();
}
}).start();

}

终于得到了答案:D/CONNECTION:无法启动服务 org.eclipse.paho.android.service.MqttService

我不明白为什么这不起作用。也许您有一些意见。

明确一点:两个程序确实具有相同的权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

假设

我想,我必须使用更多的 asycTasks 来解决这个问题。

输入/链接

如果您能分享一些链接,那就太棒了。在最好的情况下有一些例子。我只找到了在单个 MainActivity 中运行的示例。

最佳答案

错误

D/CONNECTION: cannot start service org.eclipse.paho.android.service.MqttService

可能表示条目

<service android:name="org.eclipse.paho.android.service.MqttService">
</service>

AndroidManifest.xml 中丢失

关于android - MqttAndroidClient - onFailure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44783241/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com