Добрый вечер, поэтому я занимаюсь разработкой приложения с использованием Android Studio и хочу получить выходные данные терминала из Raspberry, поэтому я использую JSch для подключения по ssh.Тем не менее, поскольку моя малиновая задача является циклической (с частотой 1 Гц), я хочу получить конечные линии, как только они израсходованы Rasp.Другими словами, мне нужно отображать выходные данные, пока функция Android все еще работает, чтобы получить следующие строки:
На данный момент следующий код отображает только первую строку выходного сигнала терминала, а для затемненияпричина (для меня), а не следующие строки!Надеюсь, вы сможете мне помочь.
Ниже приведен мой код, который работает не так, как мне хотелось бы:
Я вызываю асинхронную задачу RetrieveFeedTask при нажатии кнопки отправки.:
send_button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
String command = “./SomeProgrammeToExecute”;
String command_type = "send";
new RetrieveFeedTask().execute(command, command_type);
command_edit_text.setText("");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//Something else
}
return true;
}
});
}
Асинхронная задача выглядит следующим образом:
class RetrieveFeedTask extends AsyncTask<String, Void, String> {
private Exception exception;
protected String doInBackground(String... params) {
try {
String hostname = "192.168.0.14";
String username = "root";
String password = "pi";
String result = runCommand(hostname, username, password, params[0], params[1]);
return result;
} catch (Exception e) {
this.exception = e;
return e.toString();
}
}
}
И этот класс вызывает функцию runCommand, котораявернуть выходную строку, а также установить TextView с выходной строкой:
public String runCommand(String hostname, String username, String password, String command, String command_type)
throws JSchException, IOException {
jSch = new JSch();
int port = 22;
session = jSch.getSession(username, hostname, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect(30000);
channel = (ChannelExec) session.openChannel("exec");
channel.setPty(true);
channel.setCommand(command);
in = new BufferedReader(new InputStreamReader(channel.getInputStream(), "utf8"));
out = channel.getOutputStream();
channel.connect();
String result_output = "";
if(command_type == "send"){
String line = null;
String current_terminal = "";
while(true) {
line = in.readLine();
if(line != null){
current_terminal = terminal_textView.getText().toString();
result_output = current_terminal + "\n" + result_output + "\n" + line;
terminal_textView.setText(result_output);
}else{
if(channel.isClosed()){
current_terminal = terminal_textView.getText().toString();
result_output = current_terminal + "\n" + result_output + "\n" + channel.getExitStatus();
terminal_textView.setText(result_output);
break;
}else{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
current_terminal = terminal_textView.getText().toString();
result_output = current_terminal + "\n" + result_output + "\n" + e.toString();
terminal_textView.setText(result_output);
}
}
}
}
}
channel.disconnect();
session.disconnect();
return result_output;
}