【发布时间】:2014-10-23 06:12:30
【问题描述】:
当我尝试在 3G 连接上运行我的应用程序时,它会尝试连接大约 1 或 2 分钟,然后抛出异常,它在 wifi 上运行良好,但在 3G 上却不行。我尝试过在不同网络上使用不同人的电话。为什么连接不上?
它抛出的异常是 MQTT 异常
Unable to connect to server
这是我称之为创建新线程的 MQTT 类:
public class MQTTService extends Service {
ContextWrapper c = this;
private MqttClient mqttClient;
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
Thread background = new Thread(new Runnable() {
final String BROKER_URL = "tcp://gateway.thebroker.com:1883";
final String clientId = "android-client90";
public void run() {
Looper.prepare();
try{
MqttClient mqttClient = new MqttClient(BROKER_URL, clientId, new MemoryPersistence());
MqttConnectOptions opts = new MqttConnectOptions();
opts.setKeepAliveInterval(600);
opts.setUserName("nabin");
opts.setPassword("M4rk3r".toCharArray());
Log.e("Before Callback", "Connecting..."); // Code works till here if callback is set then errors galore
//mqttClient.setCallback(handler.obtainMessage());
mqttClient.setCallback(new PushCallback(c));
mqttClient.connect(opts);
mqttClient.subscribe("house/pill/notification/bad");
mqttClient.subscribe("house/pill/notification/good");
mqttClient.subscribe("house/pill/notification/skip");
mqttClient.subscribe("house/pill/notification/alert");
mqttClient.subscribe("house/pill/notification/snooze");
mqttClient.subscribe("house/pill/schedule/response");
mqttClient.subscribe("house/pill/nextPill/response");
Log.e("CONNECTED!", "COOOONNECTED!!!!!");
}catch(MqttException e){
Log.e("Internet Error", "No connection available " + e.getMessage() );
}catch(Exception e){
Log.e("ERRRRRORRRRR ", e.getMessage());
}
}
});
background.start();
}
}
感谢您的帮助!
【问题讨论】: