[英]How to call second class method from one class and print output of method inside second class in java
MQConnection.java
package com.example.rabbitmq;
import android.os.StrictMode;
import android.util.Log;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
final static String QUEUE_NAME = "hello2";
final static String EXCHANGE_NAME = "logs_monitor";
final static String HOST = "localhost";
final static int PORT = 5672;
final static Connection connection;
final static Channel channel;
public class MqConnection {
public void setupConnection() {
Log.d("Debug","SetupConnection connected");
ConnectionFactory factory = new ConnectionFactory();
try {
factory.setHost(HOST);
factory.setPort(PORT);
// factory.setVirtualHost("/");
// factory.setUsername(USERNAME);
// factory.setPassword(PASSWORD);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
connection = factory.newConnection();
channel = connection.createChannel();
} catch (IOException | TimeoutException e) {
throw new RuntimeException("Rabbitmq problem", e);
}
}
}
MQConsume.java
package com.example.rabbitmq;
import android.util.Log;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.DeliverCallback;
import java.io.IOException;
import static com.example.rabbitmq.setVariables.EXCHANGE_NAME;
//import static com.example.rabbitmq.setVariables.QUEUE_NAME;
import static com.example.rabbitmq.setVariables.QUEUE_NAME;
import static com.example.rabbitmq.setVariables.channel;
import com.example.rabbitmq.MainActivity.*;
public class MqConsume {
static String message = "";
public static void consumeMessages(String[] args) throws IOException {
Log.d("Debug","Before rabbitmq publish");
// channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
channel.queueDeclare(QUEUE_NAME,true,false,false,null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
message = new String(delivery.getBody(), "UTF-8");
Log.d("Debug","Received");
Log.d("Debug",message);
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
}
}
MainActivity.java
package com.example.rabbitmq;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MqConnection mqConnection = new MqConnection();
mqConnection.setupConnection();
MqConsume mqConsume = new MqConsume();
final TextView received_msg;
received_msg = findViewById(R.id.received_messages);
System.out.println(received_msg);
}
}
這里我使用帶有后端java代碼的Android Studio代碼來連接rabbitmq。 如何從MainActivity.java調用MqConsume.java中的消息參數
我需要在MqConsume.java的MainActivity 類中打印消息參數。
我嘗試在MainActivity.java中調用MqConsume.java並在MainActivity.java中打印消息參數。
有沒有辦法將數據從一個類獲取到另一個類?
看起來您想從異步 API 中檢索數據並在您的活動中使用它。 您可以通過聲明自定義接口並將其實例傳遞給進行 API 調用的函數來實現此目的。 在這里查看有關為什么需要這樣做的更多詳細信息。
public class MqConsume {
// declare a callback interface
public interface GotMessage {
void gotMessage(String msg);
}
// pass the interface in here and call it when
// the message is received
public static void consumeMessages(String[] args, GotMessage callback) throws IOException {
Log.d("Debug","Before rabbitmq publish");
channel.queueDeclare(QUEUE_NAME,true,false,false,null);
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "");
DeliverCallback deliverCallback = (consumerTag, delivery) -> {
String message = new String(delivery.getBody(), "UTF-8");
Log.d("Debug","Received");
Log.d("Debug",message);
callback.gotMessage(message);
};
channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
}
}
然后,如果您從 MainActivity 調用它,您可以像這樣傳入回調:
final TextView received_msg = findViewById(R.id.received_messages);
// create the interface in the activity and set
// view text inside it
MqConsume.consumeMessages(args, new MqConsume.GotMessage() {
@Override
void gotMessage(String message) {
received_msg.setText(message);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.