Olá pessoal, estou com um problema, preciso realizar um ping em um servidor pelo android…
estou usando as seguinte função que achei na net:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class Ping extends Activity {
public String success_msg = null;
public String error_msg = null;
public Boolean success = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ping);
Process proc = run("ping -c 2 localhost");
try {
if (proc.waitFor() == 0)
success = true;
success_msg = getResult(proc.getInputStream());
error_msg = getResult(proc.getErrorStream());
} catch (InterruptedException ex) {
success = false;
print("Error in execute command: " + ex.toString());
} catch (NullPointerException ex) {
success = false;
print("Error in execute command: " + ex.toString());
}
}
public String result() {
return "";
}
public Process run(String command) {
Process proc = null;
try {
proc = Runtime.getRuntime().exec("sudo");
DataOutputStream dataProc = new DataOutputStream(proc.getOutputStream());
dataProc.writeBytes("exec " + command + "\n");
dataProc.flush();
} catch (Exception ex) {
print(ex.getMessage());
success = false;
proc = null;
}
return proc;
}
private String getResult(InputStream istream) {
String line = null;
StringBuffer result = new StringBuffer();
BufferedReader dataIstream = new BufferedReader(new InputStreamReader(
istream));
try {
while ((line = dataIstream.readLine()) != null) {
result.append(line + "\n");
}
dataIstream.close();
} catch (Exception ex) {
success = false;
print(ex.getMessage());
}
return result.toString();
}
public void print(String msg) {
Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show ();
}
}
essa função esta retornando dois erros no Toast, segue:
a primeira: Error running exec(). Command: [sudo] working directory : null Environment: null
a segunda: Error in execute command: java.lang.NullPointerException
me ajudem…