【问题标题】:When i used mqtt client in other activity they show me error当我在其他活动中使用 mqtt 客户端时,他们向我显示错误
【发布时间】:2019-08-29 07:24:35
【问题描述】:

当我在其他活动中使用 mqtt 客户端时,它们向我显示错误,当我在 OnDestroy 中关闭客户端然后在不同活动中使用客户端时,它没有给出错误但 setactioncallback 没有工作它没有成功也没有失败

主要活动

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

    username = findViewById(R.id.username);
    password = findViewById(R.id.password);
    login = findViewById(R.id.btn);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String clientId = MqttClient.generateClientId();
             client =
                    new MqttAndroidClient(MainActivity.this, "tcp://broker.hivemq.com:1883",
                            clientId);

            try {
                MqttConnectOptions options = new MqttConnectOptions();
                options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);

                IMqttToken token = client.connect();
                token.setActionCallback(new IMqttActionListener() {
                    @Override
                    public void onSuccess(IMqttToken asyncActionToken) {
                        // We are connected
                        Log.d(TAG, "onSuccess");

                        gotosubscribelist();
                    }

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

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

}

private void gotosubscribelist()
{
    Intent intent = new Intent(this,SubscribelistActivity.class);
    intent.putExtra("client", String.valueOf(client));
    startActivity(intent);
    finish();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    client.unregisterResources();
   client.close();
}

订阅活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_subscribelist);

    try {
        MainActivity.client.connect();
    } catch (MqttException e) {
        e.printStackTrace();
    }

    channel = findViewById(R.id.channel);
    subscribe = findViewById(R.id.subscribe);

    mRec = (RecyclerView) findViewById(R.id.recyclerview);
    newlist = new ArrayList<>();
    adapter = new ChannelAdapter(this,newlist);

    linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    mRec.setHasFixedSize(true);
    mRec.setLayoutManager(linearLayoutManager);
    mRec.setAdapter(adapter);

    subscribe.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            subscribe();
        }
    });
}

private void subscribe()
{
    Log.e("hi","1");
    final String topic = channel.getText().toString();
    int qos = 1;
    try {
        IMqttToken subToken = MainActivity.client.subscribe(topic, qos);
        subToken.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                Log.e("suc","create");
                newlist.add(topic);
            }

            @Override
            public void onFailure(IMqttToken asyncActionToken,
                                  Throwable exception) {
             Log.e("e",exception.getMessage());

            }
        });

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

@Override
protected void onDestroy() {
    super.onDestroy();

    MainActivity.client.unregisterResources();
    MainActivity.client.close();
}

我的问题是如果我在 onDestroy 中删除 client.unregisterResources 和 client.close 然后它会显示

E/ActivityThread: Activity com.example.mqtt.UI.MainActivity has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@7ce0751 that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.example.mqtt.UI.MainActivity has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@7ce0751 that was originally bound here

当我将 client.unregisterResources 和 client.close 放入 onDestroy 时,它没有显示错误,但在订阅功能中它没有运行 onsuccess 和 onfailure,请给出一些建议

渠道活动

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_channel);

    try {
        MainActivity.client.connect();
        MainActivity.client.isConnected();
    } catch (MqttException e) {
        e.printStackTrace();
    }

    message = findViewById(R.id.msg);
    publish = findViewById(R.id.publish);

    name = getIntent().getExtras().get("currentchannelname").toString();

    Rec = (RecyclerView) findViewById(R.id.recyclerview_msg);
    newlist = new ArrayList<>();
    adapter = new msgAdapter(this,newlist);

    linearLayoutManager = new LinearLayoutManager(getApplicationContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    Rec.setHasFixedSize(true);
    Rec.setLayoutManager(linearLayoutManager);
    Rec.setAdapter(adapter);

    getmessage();

    publish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            publishmsg();
        }
    });

}

private void publishmsg()
{
    String topic = name;
    String payload = message.getText().toString().trim();
    byte[] encodedPayload = new byte[0];
    try {
        encodedPayload = payload.getBytes("UTF-8");
        MqttMessage message = new MqttMessage(encodedPayload);
        MainActivity.client.publish(topic, message);
    } catch (UnsupportedEncodingException | MqttException e) {
        e.printStackTrace();
    }
}

private void getmessage()
{
    MainActivity.client.setCallback(new MqttCallback() {
        @Override
        public void connectionLost(Throwable cause) {

        }

        @Override
        public void messageArrived(String topic, MqttMessage message) throws Exception {
            newlist.add(message.toString());
        }

        @Override
        public void deliveryComplete(IMqttDeliveryToken token) {

        }
    });
    adapter.notifyDataSetChanged();
}

@Override
protected void onDestroy() {
    super.onDestroy();

    MainActivity.client.unregisterResources();
    MainActivity.client.close();
}

【问题讨论】:

    标签: java android mqtt paho


    【解决方案1】:

    删除

    @Override
    protected void onDestroy() {
    super.onDestroy();
    
    MainActivity.client.unregisterResources();
    MainActivity.client.close();
    }
    

    来自SubscribeActivity

    【讨论】:

    • 但我在频道活动频道活动中也使用了客户端,通过点击 recyclerview 项目(订阅频道列表)开始
    • 只在MainActivity中使用上述方法
    • 我从订阅和频道活动中删除了关闭,但 setactioncallback 不适用于订阅频道
    • 总是针对一个问题提出一个问题。你的崩溃是由于这个,最好为它发布另一个问题@mayur_123
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2013-06-12
    • 2012-06-14
    相关资源
    最近更新 更多