我的网络中有一个ActiveMQ代理,在1883年(mqtt)收听。如果我使用简单的Java应用程序连接到该代理,并且mqtt-client-0.4.0.jar库一切正常。
现在我想用Android设备连接到该代理。不幸的是,我无法管理它以获得成功的连接。我总是收到以下错误:
03-23 11:08:15.679 24572-24572/com.my.package E/WorkerMQTT: onFailure: Unable to connect to server (32103) - java.net.ConnectException: failed to connect to /192.192.192.10 (port 1883) after 30000ms: isConnected failed: ENETUNREACH (Network is unreachable)
03-23 11:08:15.687 24572-24572/com.my.package W/System.err: Unable to connect to server (32103) - java.net.ConnectException: failed to connect to /192.192.192.10 (port 1883) after 30000ms: isConnected failed: ENETUNREACH (Network is unreachable)
03-23 11:08:15.695 24572-24572/com.my.package W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:79)
03-23 11:08:15.695 24572-24572/com.my.package W/System.err: at org.eclipse.paho.client.mqttv3.internal.ClientComms$ConnectBG.run(ClientComms.java:590)
03-23 11:08:15.695 24572-24572/com.my.package W/System.err: at java.lang.Thread.run(Thread.java:856)
03-23 11:08:15.695 24572-24572/com.my.package W/System.err: Caused by: java.net.ConnectException: failed to connect to /192.192.192.10 (port 1883) after 30000ms: isConnected failed: ENETUNREACH (Network is unreachable)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:224)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: at libcore.io.IoBridge.connectErrno(IoBridge.java:161)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: at libcore.io.IoBridge.connect(IoBridge.java:112)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: at java.net.Socket.connect(Socket.java:842)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: at org.eclipse.paho.client.mqttv3.internal.TCPNetworkModule.start(TCPNetworkModule.java:70)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: ... 2 more
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: Caused by: libcore.io.ErrnoException: isConnected failed: ENETUNREACH (Network is unreachable)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: at libcore.io.IoBridge.isConnected(IoBridge.java:208)
03-23 11:08:15.703 24572-24572/com.my.package W/System.err: ... 8 more
我正在使用调用MqttWorker extends Thread{
的Android服务。
同样作为图书馆我正在使用:
compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
exclude module: 'support-v4'
}
按照我的工作人员的代码:
@Override
public void run() {
String clientId = "AndroidSub_7";
try {
this.client =
new MqttAndroidClient(this.context, "tcp://192.192.192.10:1883", clientId);
this.options = new MqttConnectOptions();
this.options.setWill("clients/clienterrors", "crashed".getBytes(),2,true); // handle uncleanly closed subscribers
this.client.setCallback(new ClientCallbacks()); // set new callbacks
this.client.connect(options);
IMqttToken conToken;
conToken = client.connect(options, this.context, new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.d(TAG, "onSuccess");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.e(TAG, "onFailure: " + exception);
exception.printStackTrace();
}
});
} catch (MqttException e) {
Log.e(TAG,"Connection to broker failed:" + e);
}
}
到目前为止我尝试了什么:
AndroidManifest.xml
WAKE_LOCK /INTERNET / WRITE_EXTERNAL_STORAGE / ACCESS_NETWORK_STATE
MqttAndroidClient
或MqttClient
,则无关紧要。错误是一样的。我希望有人可以帮助我。我没有想法如何解决这个问题。
答案 0 :(得分:1)
我使用的是真正的Android设备。
为了获得有关网络的更多信息,我通过adb shell
连接到Android设备。从ping命令我得到以下内容
From 192.192.192.1 icmp_seq=2 Destination Net Unreachable
。奇怪的是,它仍然使用套接字测试。总的来说,我重新启动设备,现在它可以运行了。答案可能不那么令人满意,但至少它现在有效。