简体   繁体   English

适用于Android的MQTT Java应用程序中的setCallback抛出错误

[英]setCallback throwing error in MQTT java application for Android

I am trying to build an MQTT "listener" app for Android and have used this sample application as a model. 我正在尝试为Android构建MQTT“侦听器”应用程序,并已将此示例应用程序用作模型。 However the original sample throws a "Network on Main Activity" runtime error and so I am forced to an AsyncTask class. 但是,原始示例将引发“主要活动上的网络”运行时错误,因此我被迫使用AsyncTask类。

The main App runs as a service : LWMQService.java [ relevant part of code here ] 主应用程序作为服务运行:LWMQService.java [代码的相关部分在此处]

package org.iothub.lightwatchmq;

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.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import org.iothub.lightwatchmq.PushCallback;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import android.provider.Settings.Secure;

public class LWMQService extends Service implements SensorEventListener {

    -----

    String BROKER_URL = "tcp://test.mosquitto.org:1883";
    String TOPIC = "iothub";
    String TOPIC2 = "iothub2";
    String ClientID ;

    private MqttClient client;

   @Override
   public IBinder onBind(Intent arg0) {
      return null;
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {

      ------

       ClientID = Secure.getString(getContentResolver(), Secure.ANDROID_ID); 

       try {

           client = new MqttClient(BROKER_URL, ClientID, new MemoryPersistence());
       } catch (MqttException e) {
           e.printStackTrace();
           System.exit(1);
       }
       new mConnect ().execute();
     //  new mTransmit ().execute();
       new mListen().execute();;

      Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
      Log.d(LogMsgID, "LightWatch Service started");
      return START_STICKY;
   }

   @Override
   public void onDestroy() {
      -----
      return;
   }

   private class mConnect extends AsyncTask<Void, Void, Void> {
         protected Void doInBackground(Void...dummy) {
             try {

                 client.connect();

             } catch (MqttException e) {
                 e.printStackTrace();
                 Log.d(LogMsgID, "Connect Failed");
                 System.exit(1);
             }
            return null;
         }
    }

   public class mListen extends AsyncTask<Void, Void, Void> {
         protected Void doInBackground(Void...dummy) {
             try {
                 client.setCallback(new PushCallback(this));
                 client.subscribe(TOPIC2);

             } catch (MqttException e) {
                 e.printStackTrace();
                 Log.d(LogMsgID, "Listen Failed");
                 System.exit(1);
             }
            return null;
         }
    }


   private class mTransmit extends AsyncTask<Void, Void, Void> {
         protected Void doInBackground(Void...dummy) {

            --------
         }

     }

   @Override
      public final void onAccuracyChanged(Sensor sensor, int accuracy) {
        // Do something here if sensor accuracy changes.
      }

    @Override
      public final void onSensorChanged(SensorEvent event) {
        // The light sensor returns a single value.
        ---
      }
}

the mConnect and the mTransmit( publish) classes compile correctly and work fine BUT the mListen ( subscribe) class i get a compile error that states "The constructor PushCallback(LWMQService.mListen) is undefined" showing on the following line in the class mConnect和mTransmit(publish)类可以正确编译并正常工作,但mListen(subscribe)类却出现编译错误,指出该类的下一行显示“构造函数PushCallback(LWMQService.mListen)未定义”

client.setCallback(new PushCallback(this));

the PushCallback class is available in a different file : PushCallback.java PushCallback类在另一个文件中可用:PushCallback.java

package org.iothub.lightwatchmq;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.util.Log;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.iothub.lightwatchmq.LWMQService.mListen;

public class PushCallback implements MqttCallback {

    String LogMsgID = "iothub: ";
    private ContextWrapper context;

    public PushCallback(ContextWrapper context) {

        this.context = context;
        Log.d(LogMsgID, "PCB-1");
    }

    @Override
    public void connectionLost(Throwable cause) {
        //We should reconnect here
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void messageArrived(String Topic, MqttMessage message) throws Exception {
        // TODO Auto-generated method stub

        final Intent intent = new Intent(context, MainActivity.class);
        final PendingIntent activity = PendingIntent.getActivity(context, 0, intent, 0);
        String RecdMessage = new String(message.getPayload());
        Log.d(LogMsgID, RecdMessage);

    }
}

I would admit that I am not very good at Java and maybe making some obvious error but would be grateful if someone can please help me out with this error. 我承认我不太擅长Java,也许会犯一些明显的错误,但是如果有人可以帮助我解决此错误,我将不胜感激。

In the scope where you call client.setCallback(new PushCallback(this)); 在调用client.setCallback(new PushCallback(this)); this is the AsyncTask , as AsyncTask class does not extend ContextWrapper it will not match the constructor. AsyncTask ,因为AsyncTask类不会扩展ContextWrapper,因此它将与构造函数不匹配。

Change the line to be 将行更改为

client.setCallback(new PushCallback(LWMQService.this));

to grab the right context object 获取正确的上下文对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM