gpt4 book ai didi

android - 订阅 MqttAndroidClient 上的主题时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 15:57:49 24 4
gpt4 key购买 nike

我使用了来自 Paho Eclipse 存储库的 Android 服务 jar。我在客户端连接后订阅任何主题时遇到 NUllPOinterException。

    public void connect(View view) {

MqttConnectOptions conOpt = new MqttConnectOptions();

String uri = "tcp://" + host + ":" + port;
boolean cleanSession = true;
// int timeout = 60 ;
// int keepalive = 60 ;
clientHandle = uri + clientId;

MqttAndroidClient client;
client = Connections.getInstance(this)
.createClient(this, uri, clientId);

Connection conn = new Connection(clientHandle, clientId, host, port,
this, client, sslConnection);

conOpt.setCleanSession(cleanSession);
// conOpt.setConnectionTimeout(timeout);
conOpt.setKeepAliveInterval(100);;

MqttCallbackHandler callback = new MqttCallbackHandler(this,
MainActivity.this, clientHandle);

conn.addConnectionOptions(conOpt);
Connections.getInstance(this).addConnection(conn);
String[] topics = { "hello", "hi" };
int qos = 0;
try {
IMqttToken t = client.connect(conOpt, null, new ActionListener(this,
Action.CONNECT, uri, (String[]) null));
//t.waitForCompletion(90);
client.setCallback(callback);
IMqttToken token = Connections
.getInstance(this)
.getConnection(clientHandle)
.getClient()
.subscribe(
"shatinTopic",
qos,
null,
new ActionListener(this, Action.SUBSCRIBE, uri,
topics));
Log.d("SUBSCRIBE", token.getResponse().toString());
} catch (MqttException e) {
e.getCause();
} catch (NullPointerException e) {
e.getCause();
} catch (Exception e) {
e.getCause();
}
}

错误:

11-09 15:17:24.702: E/AndroidRuntime(26671): Caused by: java.lang.NullPointerException
11-09 15:17:24.702: E/AndroidRuntime(26671): at org.eclipse.paho.android.service.MqttAndroidClient.subscribe(MqttAndroidClient.java:788)

请问有人能帮忙指出哪里出了问题吗。

提前致谢!

最佳答案

从 paho 开发邮件列表得到这个答案。

Because connect is asynchronous, and the connection may not established the time you call subscribe. you should either waitForCompletion or call subscribe in IMqttActionListener.onSuccess, if you are using IMqttToken connect(MqttConnectOptions options, Object userContext, IMqttActionListener callback)

虽然 waitForCompletion 给出了服务器超时错误,但订阅监听器的 onSuccess 方法解决了这个问题。

关于android - 订阅 MqttAndroidClient 上的主题时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827020/

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