I am doing an exercise in Android and I want to display value of a ping. I followed an existing answer (this topic). The fact is that the code that the person gave doesn't display any information (I toasted it to test the return value of the function "ping"). I called the function like this :
Toast.makeText(getApplicationContext(), ping("http://www.stackoverflow.com"), Toast.LENGTH_SHORT).show();
Any other kind of code to simply display ping value ?
I precise that my code is free of any other process, I just want to finally display this result in a small textView.
Here is my code.
MainActivity.java
private String ping(String url) {
String str = "";
try {
Process process = Runtime.getRuntime().exec(
"/system/bin/ping -c 8 " + url);
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
int i;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((i = reader.read(buffer)) > 0)
output.append(buffer, 0, i);
reader.close();
// body.append(output.toString()+"\n");
str = "ping : " + output.toString();
// Log.d(TAG, str);
} catch (IOException e) {
// body.append("Error\n");
e.printStackTrace();
}
return str;
}
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
toggleButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), ping("http://www.stackoverflow.com"), Toast.LENGTH_SHORT).show();
});
}); }