无法在Android Studio上运行我的MQTT应用程序

时间:2015-07-29 13:21:06

标签: java android android-studio mqtt

我正在测试个人MQTT应用程序,但是当我点击连接按钮时它失败了...有一个例外,但我不知道它来自哪里。

这是我的代码

MainActivity.java:

package com.application.phoste.homecontrol;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
    private Paho paho = null;
    EditText topic = null;
    EditText message = null;
    Button connect = null;
    Button send = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        paho = new Paho();

        topic = (EditText) findViewById(R.id.topic);
        message = (EditText) findViewById(R.id.message);

        connect = (Button) findViewById(R.id.connect);
        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                paho.connect();
                String res = (paho.isConnected()) ? "Connected" : "Not Connected";
                Toast toast = Toast.makeText(getApplicationContext(), res, Toast.LENGTH_SHORT);
                toast.show();
            }
        });
        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                paho.publish(topic.getText().toString(), message.getText().toString());
                Toast toast = Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }

    @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);
    }
}

Paho.java:

package com.application.phoste.homecontrol;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class Paho implements MqttCallback {

    private MqttClient client;
    private static final String BROKER = "tcp://192.168.1.189:1883";
    private static final int QOS = 2;

    public void connect() {
        try {
            client = new MqttClient(BROKER, MqttClient.generateClientId());
            MqttConnectOptions options = new MqttConnectOptions();
            options.setConnectionTimeout(1);
            client.setCallback(this);
            client.connect();
        } catch (MqttException e) {
            e.getMessage();
        }
    }

    public void disconnect() {
        if (client.isConnected()) {
            try {
                client.disconnect();
            } catch (MqttException e) {
                e.getMessage();
            }
        }
    }

    public boolean isConnected() {
        return client.isConnected();
    }

    public void publish(String topic, String m) {
        try {
            MqttMessage message = new MqttMessage(m.getBytes());
            message.setQos(2);
            client.publish(topic, message);
        } catch (MqttException e) {
            e.getMessage();
        }
    }

    @Override
    public void connectionLost(Throwable throwable) {

    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
}

例外:

07-29 15:17:23.988  15718-15718/com.application.phoste.homecontrol E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.application.phoste.homecontrol, PID: 15718
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.eclipse.paho.client.mqttv3.MqttClient.isConnected()' on a null object reference
            at com.application.phoste.homecontrol.Paho.isConnected(Paho.java:44)
            at com.application.phoste.homecontrol.MainActivity$1.onClick(MainActivity.java:35)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19748)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

我还将<uses-permission android:name="android.permission.internet"/>行添加到AndroidManifest.xml

我哪里错了?

编辑:

我刚刚意识到在异常被抛出之前我有一些警告信息......这里是:

07-29 17:26:52.673  32706-32706/com.application.phoste.homecontrol W/System.err﹕ MqttException (0)
07-29 17:26:52.673  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:80)
07-29 17:26:52.673  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:286)
07-29 17:26:52.674  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at org.eclipse.paho.client.mqttv3.MqttAsyncClient.<init>(MqttAsyncClient.java:167)
07-29 17:26:52.674  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:224)
07-29 17:26:52.674  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at org.eclipse.paho.client.mqttv3.MqttClient.<init>(MqttClient.java:136)
07-29 17:26:52.675  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at com.application.phoste.homecontrol.MainActivity.onCreate(MainActivity.java:35)
07-29 17:26:52.675  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5933)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.app.ActivityThread.access$900(ActivityThread.java:147)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
07-29 17:26:52.676  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
07-29 17:26:52.678  32706-32706/com.application.phoste.homecontrol W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

我真的希望有人能帮助我。

我还改变了我的代码来完成MainActivity中的所有操作:

package com.application.phoste.homecontrol;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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


public class MainActivity extends Activity {
    EditText topic = null;
    EditText message = null;
    Button send = null;
    Button debug = null;
    MqttClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            client = new MqttClient("tcp://m2m.eclipse.org:1883", MqttClient.generateClientId());
            MqttConnectOptions options = new MqttConnectOptions();
            options.setConnectionTimeout(1);
            client.connect();
        } catch (MqttException e) {
            e.printStackTrace();
        }

        topic = (EditText) findViewById(R.id.topic);
        message = (EditText) findViewById(R.id.message);

        send = (Button) findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    MqttMessage m = new MqttMessage(message.getText().toString().getBytes());
                    m.setQos(2);
                    client.publish(topic.getText().toString(), m);
                    Toast toast = Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT);
                    toast.show();
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }
        });

        debug = (Button) findViewById(R.id.debug);
        debug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String res = (client == null) ? "Null" : "Not Null";
                Toast toast = Toast.makeText(getApplicationContext(), res, Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }

    @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);
    }
}

但它仍然无法正常工作,我从调试按钮获得Null Toast。

2 个答案:

答案 0 :(得分:0)

您的&#34;客户&#34;当您尝试对其执行某些操作时,对象未初始化。

移动此行:client = new MqttClient(BROKER, MqttClient.generateClientId());

到应用程序的其他位置(可能是onCreate()),以确保在访问它之前正确初始化变量.i

答案 1 :(得分:0)

好的,所以你需要设置MQTT文件持久化路径,这需要对SD卡的写访问权以及你给它的路径。

https://www.eclipse.org/paho/files/javadoc/index.html?org/eclipse/paho/client/mqttv3/persist/MqttDefaultFilePersistence.html

您还需要添加访问文件系统的权限。