MqttClient 对象的同步和异步接口不起作用
我已经创建了一个类型,如下代码所示,我创建了一个客户端并定义了它的.问题是,client
MqttClient
Asynchronous callback
1-当我运行程序m时,出现,但我没有收到响应fr0m或来自o,为什么?哇,我在代码中做错了。System.out.println("Client is Connected");
onSuccess
onFailure
2-i实现了接口,但由于我有一个类型的客户端,我不能使用这个接口。我试图使用,但因为我为java编程,而不是我不能使用它。如何使用界面?static IMqttAsyncClient asynchClientCB = new IMqttAsyncClient()
MqttClient
IMqttAsyncClient
mqttAsynchClien
Android
IMqttAsyncClient
Update_1
在下面的代码“Updated_code_1”中,我稍微修改了代码,但我希望每次我成功连接到同步回调中的消息时都要打印,并且在onnection终止的情况下打印同步clobck中的消息,例如当我故意断开网络时。但是在 ru 时间当我连接到 时,既不支付也不支付任何东西。那么,它们的设计目的是什么呢?broker
onSuccess
onFailure
broker
onSuccess
onFailur
*Update_2_17_Dec_2014
我有一个查询,可能会引导我们找到解决方案,也就是说,如果我通过有线/无线网络连接到代理,这有什么关系吗?这会改变同步和异步侦听器的行为吗?
Updated_1_code:
MqttConnectOptions opts = getClientOptions();
client = MQTTClientFactory.newClient(broker, port, clientID);
if (client != null) {
System.out.println("Client is not Null");
client.setCallback(AsynchCallBack);
if (opts != null) {
iMQTTToken = client.connectWithResult(opts);
publishMSG(client, TOPIC,"010101".getBytes(), QoS, pub_isRetained);
iMQTTToken.setActionCallback(synchCallBack);
if (client.isConnected()) {
System.out.println("Client CONNECTED.");
publishMSG(client, TOPIC,"010101".getBytes(), QoS, pub_isRetained);
}
}
}
....
....
....
....
IMqttToken iMQTTToken = new IMqttToken() {
@Override
public void waitForCompletion(long arg0) throws MqttException {
// TODO Auto-generated method stub
System.out.println("@waitForCompletion(): waiting " + (arg0 * 1000) + " seconds for connection to be established.");
}
@Override
public void waitForCompletion() throws MqttException {
// TODO Auto-generated method stub
System.out.println("@waitForCompletion(): waiting for connection to be established.");
}
@Override
public void setUserContext(Object arg0) {
// TODO Auto-generated method stub
}
@Override
public void setActionCallback(IMqttActionListener arg0) {
// TODO Auto-generated method stub
arg0.onSuccess(iMQTTToken);
//System.out.println(" " + arg0.onSuccess());
//System.out.println(" " + arg0.onSuccess(iMQTTToken));
iMQTTToken.setActionCallback(synchCallBack);
}
@Override
public boolean isComplete() {
// TODO Auto-generated method stub
return false;
}
@Override
public Object getUserContext() {
// TODO Auto-generated method stub
return null;
}
@Override
public String[] getTopics() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean getSessionPresent() {
// TODO Auto-generated method stub
return false;
}
@Override
public MqttWireMessage getResponse() {
// TODO Auto-generated method stub
return null;
}
@Override
public int getMessageId() {
// TODO Auto-generated method stub
return 0;
}
@Override
public int[] getGrantedQos() {
// TODO Auto-generated method stub
return null;
}
@Override
public MqttException getException() {
// TODO Auto-generated method stub
return null;
}
@Override
public IMqttAsyncClient getClient() {
// TODO Auto-generated method stub
return null;
}
@Override
public IMqttActionListener getActionCallback() {
// TODO Auto-generated method stub
return null;
}
};
IMqttActionListener synchCallBack = new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken arg0) {
// TODO Auto-generated method stub
System.out.println("@onSuccess: Connection Successful.");
}
@Override
public void onFailure(IMqttToken arg0, Throwable arg1) {
// TODO Auto-generated method stub
System.out.println("@onFailure: Connection Failed.");
setViewEnableState(Bconnect, true);
}
};
MqttCallback AsynchCallBack = new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage msg) throws Exception {
// TODO Auto-generated method stub
System.out.println("@messageArrived: Message Delivered.");
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// TODO Auto-generated method stub
System.out.println("@deliveryComplete: Delivery Completed.");
}
@Override
public void connectionLost(Throwable thrw) {
// TODO Auto-generated method stub
System.out.println("@Connection Lost: Connection Lost.");
setViewEnableState(Bconnect, true);
}
};
新客户端:
MqttConnectOptions opts = new MqttConnectOptions();
opts.setCleanSession(CS);
opts.setKeepAliveInterval(KATimer);
HashMap<Integer, WILL> LWTData = WILLFactory.newWILL("LWT", "LWT MS".getBytes(), 1, false);
opts.setWill(LWTData.get(0).getWILLTopic(),
LWTData.get(0).getWILLPayLoad(),
LWTData.get(0).getWILLQoS(),
LWTData.get(0).isWILLRetained());
client = MQTTClientFactory.newClient(IP, PORT, clientID);
if (client != null) {
System.out.println("client is not null");
client.setCallback(AsynchCB);
IMqttToken token = client.connectWithResult(opts);
if (client.isConnected()) {
System.out.println("Client is Connected");
token.setActionCallback(new IMqttActionListener() {
public void onSuccess(IMqttToken arg0) {
// TODO Auto-generated method stub
System.out.println("synchCB->@onSuccess(): Connection Successful");
try {
client.subscribe(TOPIC, QoS);
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client.disconnect();
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void onFailure(IMqttToken arg0, Throwable arg1) {
// TODO Auto-generated method stub
System.out.println("synchCB->@onFailure(): Connection Failed");
}
});
}else {
System.out.println("client is not connected");
}
}else {
System.out.println("client = null");
}
异步回调:
/**
* Asynchronous Callback to inform the user about events that might happens Asynchronously. If it is not used, any pending
* messages destined to the client would not be received.
*/
private static MqttCallback AsynchCB = new MqttCallback() {
public void messageArrived(String topic, MqttMessage msg) throws Exception {
// TODO Auto-generated method stub
System.out.println("AsynchCB->@messageArrived(): ");
System.out.println("Topic: " + topic);
System.out.println("MSG: " + msg.toString());
}
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
System.out.println("AsynchCB->@deliveryComplete(): ");
}
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
System.out.println("AsynchCB->@connectionLost(): ");
}
};