MqttAndroidClient - onFailure

时间:2017-06-27 14:43:25

标签: android android-asynctask mqtt paho

问题

为什么我可以通过mainActivity连接到我的服务器,但不能通过片段连接到我的服务器?

设置

计划1 - 工作......

现在,我有两个节目。 第一个名为 mqtt_test ,其中包括MainActivity,Button和连接类Mqtt。所以有没有片段。

通过该程序,我可以连接到我的服务器,订阅和接收消息。

第一个程序代码

public class mqtt {
private Context context;
private String broker = "tcp://10.34.5.134:1883";
private String clientId= "DefaultName";
private MqttAndroidClient client;
private ConnectionListener connectionListener;

private static final String TAG_MQTT= "MQTT";

public mqtt(Context context)
{
    this.context = context;
}
public void connect()
{
    final String clientId = MqttClient.generateClientId();

    //set options
    MqttConnectOptions options = new MqttConnectOptions();
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);

    client =
            new MqttAndroidClient(context, broker,
                    clientId);

    try {
        IMqttToken token = client.connect(options);
        token.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // We are connected
                Log.d(TAG_MQTT, "onSuccess");
                connectionListener.connected(true);
                client.setCallback(new MqttCallback() {
                    @Override
                    public void connectionLost(Throwable cause) {
                        Log.d(TAG_MQTT, "connectionLost: ");
                    }

                    @Override
                    public void messageArrived(String topic, MqttMessage message) throws Exception {
                        Log.d(TAG_MQTT, "messageArrived: Message: " + message.toString() + " with topic: " + topic);
                    }

                    @Override
                    public void deliveryComplete(IMqttDeliveryToken token) {
                        Log.d(TAG_MQTT, "deliveryComplete: ");
                    }
                });
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                // Something went wrong e.g. connection timeout or firewall problems
                Log.d(TAG_MQTT, "onFailure");
                connectionListener.connected(false);

            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }
}

public void subscribe(final String topic, int qos)
{
    if(client.isConnected())
    {
        try {
            IMqttToken subToken = client.subscribe(topic, qos);
            subToken.setActionCallback(new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    // The message was published
                    Log.d(TAG_MQTT, "onSuccess: Subscripted with topic: " + topic);
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken,
                                      Throwable exception) {
                    // The subscription could not be performed, maybe the user was not
                    // authorized to subscribe on the specified topic e.g. using wildcards
                    Log.d(TAG_MQTT, "onFailure: The subscription failed");
                }
            });
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
}
    public void publish(String topic, String payload, int qos)
    {

        byte[] encodedPayload = new byte[0];
        try {
            encodedPayload = payload.getBytes("UTF-8");
            MqttMessage message = new MqttMessage(encodedPayload);
            client.publish(topic, message);
            Log.d(TAG_MQTT, "publish: Published message:" + payload + " to topic: " + topic);
        } catch (UnsupportedEncodingException | MqttException e) {
            e.printStackTrace();
            Log.d(TAG_MQTT, "publish: Error");
        }
    }

    public void setConnectionListener(ConnectionListener connectionListener) {
        this.connectionListener = connectionListener;
    }
}

计划2 - 不起作用

在我的第二个程序中,我使用的代码几乎相同。但是,我正在使用片段进行未来业务。在一些片段中是按钮,它们正在调用正确的方法。 如果按下按钮,则会调用正确的方法,就像在第一个程序中一样。 但程序会跳过异步任务。 OnSucces或onFailure永远不会被调用。

IMqttToken token = client.connect(null, new IMqttActionListener() {
                @Override
                public void onSuccess(IMqttToken asyncActionToken) {
                    // connected
                    Log.d(TAG_CONNECTION, "onSuccess");
                    connectionListener.connected(true);
                    client.setCallback(new MqttCallback() {
                        @Override
                        public void connectionLost(Throwable cause) {
                            Log.d(TAG_CONNECTION, "connectionLost: ");
                        }

                        @Override
                        public void messageArrived(String topic, MqttMessage message) throws Exception {
                            Log.d(TAG_CONNECTION, "messageArrived: Message: " + message.toString() + " with topic: " + topic);
                        }

                        @Override
                        public void deliveryComplete(IMqttDeliveryToken token) {
                            Log.d(TAG_CONNECTION, "deliveryComplete: ");
                        }
                    });
                }

                @Override
                public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
                    Log.d(TAG_CONNECTION, "onFailure");
                    connectionListener.connected(false);
                    Log.d(TAG_CONNECTION, exception.getMessage());

                }
            });

然后我通过一个新线程调用了connect-method:

@Override
    public void doSmth() {
        new Thread(new Runnable() {
            public void run() {
                // a potentially  time consuming task
                mqttConnection.connect();
            }
        }).start();

    }

最后我得到了一个答案: D/CONNECTION: cannot start service org.eclipse.paho.android.service.MqttService

我不明白为什么这不起作用。也许你有一些意见。

要明确:两个程序都具有相同的权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

假设

我想,我必须使用更多的asycTasks来解决这个问题。

输入/链接

如果您可以分享一些链接,那将是非常棒的。在最好的情况下举一些例子。我只找到了在单个MainActivity中运行的示例。

1 个答案:

答案 0 :(得分:0)

错误

D/CONNECTION: cannot start service org.eclipse.paho.android.service.MqttService

可能表示条目

<service android:name="org.eclipse.paho.android.service.MqttService">
</service>
缺少AndroidManifest.xml中的