Paho MqttAndroidClient.connect总是失败 [英] Paho MqttAndroidClient.connect always fails

查看:955
本文介绍了Paho MqttAndroidClient.connect总是失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将消息从android service发布到本地服务器.这是基于此处中的代码段的最简单形式的部分代码.

I'd like to publish messages from an android service to a local server. Here is parts of my code in the simplest form based on snippets from here.

MemoryPersistence memPer;
MqttAndroidClient client;

@Override
public IBinder onBind(Intent intent) {
    memPer = new MemoryPersistence();
    client = new MqttAndroidClient(this, "tcp://192.168.1.42:1883", "clientid", memPer);

    try {
        client.connect(null, new IMqttActionListener() {

            @Override
            public void onSuccess(IMqttToken mqttToken) {
                Log.i("MQTT", "Client connected");
                Log.i("MQTT", "Topics=" + mqttToken.getTopics());

                MqttMessage message = new MqttMessage("Hello, I am Android Mqtt Client.".getBytes());
                message.setQos(2);
                message.setRetained(false);

                try {
                    client.publish("messages", message);

                    Log.i("MQTT", "Message published");

                    client.disconnect();
                    Log.i("MQTT", "client disconnected");
                } catch (MqttPersistenceException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (MqttException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(IMqttToken arg0, Throwable arg1) {
                // TODO Auto-generated method stub
                Log.i("MQTT", "Client connection failed: " + arg1.getMessage());
            }
        });
    } catch (MqttException e) {
        e.printStackTrace();
    }

    return mBinder;
}

但是总是调用onFailure函数,并且出现错误:

But the onFailure function is always called and I get the error:

I/MQTT﹕ Client connection failed: cannot start service org.eclipse.paho.android.service.MqttService

显然是由库返回的,因为'listener!= null',

Apparently returned by the library because 'listener != null', Line 410. Using the debugger, it shows that 'listener = SensorLoggerService$1@3634'. SensorLoggerService is my service.

任何想法可能出什么问题吗?非常感谢.

Any idea what could be going wrong? Thanks a lot.

推荐答案

对我来说同样的问题;就我而言,问题是<service>标记在<application>标记之外.

The same issue for me; in my case, the problem was that the <service> tag was outside the <application> tag.

一开始我有这个:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
...
<service android:name="org.eclipse.paho.android.service.MqttService">
    </service>
...
<application
    android:name="com.mycompany.myapp" ... >
...
</application>

然后我更改为:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp" >
...
<application
    android:name="com.mycompany.myapp" ... >
...
    <service android:name="org.eclipse.paho.android.service.MqttService">
    </service>

</application>

一切正常!

您还需要添加INTERNETACCESS_NETWORK_STATEWAKE_LOCK权限.

You need also to add the INTERNET, ACCESS_NETWORK_STATE and WAKE_LOCK permissions.

HTH

这篇关于Paho MqttAndroidClient.connect总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆