【问题标题】:MQTT Unable to connect to server when using 3GMQTT 使用 3G 时无法连接服务器
【发布时间】: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();
    }
   }    

感谢您的帮助!

【问题讨论】:

    标签: android mqtt


    【解决方案1】:

    如果您想从另一个网络访问您的服务器,它需要一个全球唯一的公共 IP 地址。从本地网络,通过wifi不需要公开,请求出去,在出去路由器之前找到地址。

    您在互联网提供商处获得了私有 IP 地址,这意味着他们通过路由器(NAT 网络)为您提供 IP。您可以进行交流,但没有人可以通过其他网络联系到您。

    如果您致电您的互联网提供商,您可以要求提供您的 IP 地址以将其公开。 这是我的经验,4 个支持者中有 3 个不知道公共 IP 地址是什么,他们问:你的意思是静态的? ..不..公开。说服他们。

    【讨论】:

      【解决方案2】:

      您的 wifi 连接与您的代理在同一个网络中,而您的 3G 连接尝试通过 Internet 连接到您的代理。 您的 3G 连接必须能够通过端口 1883 到达主机 gateway.thebroker.com

      【讨论】:

        【解决方案3】:

        如果您想要一个便宜的公共 IP 地址,我推荐 https://www.digitalocean.com/pricing/ 每月 5 美元,按小时计费,我的意思是... 0.007 美元/小时。然后,您可以使用任何在线教程轻松安装 Mosquitto Broker。

        或者,您可以使用公共开放服务器使用以下任何一种来测试 MQTT:http://mqtt.org/wiki/doku.php/public_brokers 问题是,它们可能很慢,因为任何人都可以使用它们。但是,是的,其他人说的是真的。您的手机通过 WLAN(无线局域网)连接到它,但是您的 3G/4G 服务无法到达该地址。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-02-22
          • 1970-01-01
          • 1970-01-01
          • 2015-09-18
          • 1970-01-01
          • 1970-01-01
          • 2018-07-04
          相关资源
          最近更新 更多