當我嘗試在3G連接上運行我的應用程序時,它嘗試連接大約1或2分鐘,然後它會引發異常,它在wifi上運行完全正常,但不在3G上。我嘗試過不同的人在不同的網絡電話。爲什麼不能連接?MQTT無法連接到服務器時使用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();
}
}
感謝您的幫助!