问题描述
我使用了真正的Android设备。要获取有关网络的更多信息,我通过接入了Android设备adb shell
。从ping命令,我得到以下信息 From
192.192.192.1 icmp_seq=2 Destination Net
Unreachable
。奇怪的是,它仍然可以使用套接字测试工作。总体而言,我
,现在可以使用了。答案可能不尽如人意,但至少现在可以了。
解决方法
我的网络中有一个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
我使用的是Android服务,该服务调用MqttWorker extends Thread{
。
另外作为图书馆,我正在使用:
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
or 都没有关系MqttClient
。错误是一样的。
我希望有一个人可以帮助我。我没办法解决这个问题了。