我正在尝试制作一个使用JSch连接到树莓派的Android应用程序。我做了三个按钮。一个连接到会话,一个断开连接,另一个发送命令。第一次,连接按钮可以正常工作。但是,断开连接后,连接按钮不起作用。我在连接按钮的onClickListerner中放置了一条打印语句,当我再次单击该按钮时,该语句甚至无法打印。
我的OnCreateMethod
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_command_page);
Intent intent = getIntent();
name = intent.getStringExtra("name");
if(name == null) {
Log.d("NAME", "The name is null");
}
commandSnd = (Button) findViewById(R.id.sndCommand);
text = (TextView) findViewById(R.id.cmdOut);
cmdInput = (EditText)findViewById(R.id.cmdInput);
input = cmdInput.getText().toString();
db = new DatabaseHelper(this);
connect = (Button) findViewById(R.id.connect);
disconnect = (Button) findViewById(R.id.disconnect);
disconnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Integer, Void, Void>() {
protected Void doInBackground(Integer... params) {
try {
disconnect(session);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
}
});;
commandSnd = (Button) findViewById(R.id.sndCommand);
text = (TextView) findViewById(R.id.cmdOut);
cmdInput = (EditText)findViewById(R.id.cmdInput);
input = cmdInput.getText().toString();
connect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.print("Button has been Clicked");
new AsyncTask<Integer, Void, Void>() {
protected Void doInBackground(Integer... params) {
try {
connect(name);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
}
});
commandSnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Integer, Void, Void>() {
protected Void doInBackground(Integer... params) {
try {
sendCommand(name, input);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
}
});
我的连接方法
public Session connect(String n) throws JSchException {
if (session == null) {
current = new JSch();
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
String[] results = getDataStrings(n);
String user = results[0];
String host = results[1];
session = current.getSession(user, host);
session.setPassword(results[2]);
session.setConfig(config);
setVisible(commandSnd, View.VISIBLE);
setVisible(disconnect, View.VISIBLE);
setVisible(connect, View.INVISIBLE);
session.connect();
session.setServerAliveInterval(10000);
} else if (session.isConnected()) {
Toast.makeText(ButtonClickedActivity.this, "You already have a session connected", Toast.LENGTH_SHORT);
}
return session;
}
我的断开连接方法
public void disconnect(Session a) {
if (a.isConnected()) {
setVisible(connect, View.VISIBLE);
setVisible(disconnect, View.INVISIBLE);
setVisible(commandSnd, View.INVISIBLE);
}
a.disconnect();
setVisible(connect, View.VISIBLE);
setVisible(disconnect, View.INVISIBLE);
setVisible(commandSnd, View.INVISIBLE);
}
最后,我的commandSend方法
public String sendCommand(String n, String command) throws JSchException {
String cmd = cmdInput.getText().toString();
if(cmd.isEmpty()) {
setText("Please insert a valid Command");
return "Insert Valid Command";
}
System.out.println(cmd);
Log.d("Command", cmd);
String[] results = getDataStrings(n);
String output;
try {
System.out.print("Channel Made");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ChannelExec channelSSH = (ChannelExec) session.openChannel("exec");
channelSSH.setOutputStream(baos);
channelSSH.setCommand(cmd);
channelSSH.connect();
try{ Thread.sleep(1000); } catch(Exception ee) {}
output = new String(baos.toByteArray());
System.out.println("CommandSent");
System.out.println("byteCreated");
System.out.println(output);
setText(output);
Log.d("Command Sent Check", output);
channelSSH.disconnect();
}
catch(Exception e) {
System.out.println("The connection failed");
setText("The connection has failed");
e.printStackTrace();
return "Please Check your Selected pi's properties";
}
return output;
}
我怀疑这与我使用AsyncTask有关,但我可能会误会。