Process command help

Put your problem here if it does not fit any of the other categories.

Process command help

Postby gazos » Fri Nov 20, 2009 2:45 pm

Hi All,

I am trying to send commands to bash and recieve text back. What is the most efficient way to do this? Also say for example i run a command like telnet or something that requires an input i.e username how would i deliver this, as at the minute if i run such a command my app hangs presumably because the ui is waiting to be updated. This is what I have at the moment:

private void ping(){

EditText edIP = (EditText) findViewById(R.id.edIP);
TextView pingOutput = (TextView) findViewById(R.id.tvPingOutput);
pingOutput.setText("");
String[] cmd = {"sh","-c","ping -c 1 " + edIP.getText()};
String readline = null;
try {
Process p = Runtime.getRuntime().exec(cmd);

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));

// read the output from the command

while (((readline = stdInput.readLine()) != null)) {
pingOutput.append("\n" + readline + "\n");
}
while (((readline = stdError.readLine()) != null)) {
pingOutput.append("\n" + readline + "\n");
}
p.destroy();

} catch (IOException e) {
// TODO Auto-generated catch block
pingOutput.append(e.getMessage());
e.printStackTrace();
}
}
gazos
Freshman
Freshman
 
Posts: 4
Joined: Fri Nov 20, 2009 2:41 pm

Top

Postby gazos » Sun Nov 22, 2009 4:44 pm

can anyone help here?
gazos
Freshman
Freshman
 
Posts: 4
Joined: Fri Nov 20, 2009 2:41 pm

put it into another thread

Postby ericyuan » Thu Dec 24, 2009 4:34 pm

call the function in another thread.
And send back the output by a handler.
ericyuan
Freshman
Freshman
 
Posts: 3
Joined: Thu Dec 24, 2009 4:31 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests