Android中用于mqtt的Eclipse paho引发错误

时间:2015-12-11 17:26:53

标签: java android mqtt

我正在尝试将MQTT eclipse paho库用于我的Android应用程序。我首先在java中编写代码并在Eclipse中进行测试。它完美地运作。但是,相同的库和函数在Android中无法正常工作。我已将我的jar添加到应用程序的libs文件夹中。我还在应用程序的gradle中添加了编译文件('libs / org.eclipse.paho.client.mqttv3-1.0.2.jar')。该应用程序正确编译,但抛出异常。 这是我的代码:

    package com.example.ankur.mqtt;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;


public class MainActivity extends ActionBarActivity {
    private TextView isConnected;
    private boolean isMQTT;
    private boolean connectionState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MQTTconnection mqtTconnection = new MQTTconnection();
        mqtTconnection.execute("Execute");


        isConnected = (TextView) findViewById(R.id.isConnected);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    private class MQTTconnection extends AsyncTask<String, Void, String>{
        @Override
        protected String doInBackground(String... params){
            boolean connectionState;
            connectionState = connectMQTT("tcp://iot.eclipse.org:1883","ankur145");
            System.out.println(connectionState);
            if (connectionState == true){
                isMQTT = "True";
                return "TRUE";
            }
               isMQTT = "False";
               return "False";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.d("DownloadClass", "Result was " + result);


        }
        protected boolean connectMQTT(String URL, String clientId){
            boolean connectionState;
            try {

                MqttClient mqClient = new MqttClient(URL, clientId);
                MqttConnectOptions connOpts = new MqttConnectOptions();
                connOpts.setCleanSession(true);
                mqClient.connect(connOpts);
                connectionState = mqClient.isConnected();
                Log.d("Connection", String.valueOf(connectionState));
                //mqClient.setCallback(this);


            } catch (MqttException me) {
                // TODO Auto-generated catch block
                System.out.println("reason "+me.getReasonCode());
                System.out.println("msg "+me.getMessage());
                System.out.println("loc "+me.getLocalizedMessage());
                System.out.println("cause "+me.getCause());
                System.out.println("excep " + me);
                me.printStackTrace();
                return false;
            }
            Log.d("ConnectionState","in ConnectionC");
            return connectionState;
        }
    }
}

这是我得到的例外:

12-12 12:44:35.466 2038-2038/com.example.ankur.mqtt I/System.out: msg MqttException
12-12 12:44:35.466 2038-2038/com.example.ankur.mqtt I/System.out: loc MqttException
12-12 12:44:35.466 2038-2038/com.example.ankur.mqtt I/System.out: cause null
12-12 12:44:35.466 2038-2038/com.example.ankur.mqtt I/System.out: excep MqttException (0)


2-11 17:57:41.981 30284-30312/com.example.ankur.mqtt W/System.err: MqttException (0)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:80)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:286)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:224)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:136)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at com.example.ankur.mqtt.MainActivity$MQTTconnection.connectMQTT(MainActivity.java:96)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at com.example.ankur.mqtt.MainActivity$MQTTconnection.doInBackground(MainActivity.java:75)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at com.example.ankur.mqtt.MainActivity$MQTTconnection.doInBackground(MainActivity.java:71)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-11 17:57:41.982 30284-30312/com.example.ankur.mqtt W/System.err:     at java.lang.Thread.run(Thread.java:818)

清单文件的权限:

uses-permission android:name="android.permission.INTERNET"

1 个答案:

答案 0 :(得分:1)

问题是因为您使用默认构造函数来创建MqttClient对象。这实例化了一个MqttDefaultFilePersistence对象,用于在传递消息时将消息写入(QOS 1或2)。即使使用写入权限,您也无法访问默认目录。

您需要使用此版本的constructor

并传递一个MqttDefaultFilePersistence对象,该对象指向您可以写入的目录或传递MemoryPersistence对象