Ping via android

Asked

Viewed 163 times

2

Is there any way to show the average ping on android? I saw some tutorials but the most I could was the following:

public void fExecutarPing(View view){
    List<String> listaResponstaPing = new ArrayList<String>();
    ArrayAdapter<String> adapterLista = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
            listaResponstaPing);

    try {
        String cmdPing = "ping -c 4 " + host;
        Runtime r = Runtime.getRuntime();
        Process p = r.exec(cmdPing);
        BufferedReader in = new BufferedReader( new InputStreamReader(p.getInputStream()));
        String inputLinhe;

        while((inputLinhe = in.readLine())!= null){
            listaResponstaPing.add(inputLinhe);
            //adiciona para cada linha
            listaPing.setAdapter(adapterLista);
        }

    } catch (Exception e) {
        Toast.makeText(this, "Erro: "+e.getMessage().toString(), Toast.LENGTH_SHORT).show();

    }


}

That shows the ping, along with the rest of the information. And what I need is for you to show only the ping average. How to do?

1 answer

2

Let us take this line of the result into consideration:

4 packets transmitted, 4 Received, 0% Packet Loss, time 3004ms

What interests you is the following part: 4 received.

To get this number, we’ll use the method index

Follow an example:

private Integer getPacotesRecebidos(final String fulltext){
    /**
     * Parte inicial do texto que vamos buscar na fullText
     */
    final String textStart = "packets transmitted,";
    /**
     * Pegamos o indice onde começa esta parte do texto mais o seu tamanho
     */
    int start_ = fulltext.indexOf(textStart) + textStart.length();
    /**
     * Pegamos o indice onde começa esta parte
     */
    int end_ = fulltext.indexOf("received,");

    /**
     * Através do substring, pegamos a parte específica da string
     */
    final String s = fulltext.substring(start_, end_);

    /**
     * Transformamos a String em um Integer (trim: remove os espaços em branco)
     */
    return Integer.valueOf(s.trim());

}

Utilizing:

@Override
public void onClick(View v) {

    try{
        String cmdPing = "ping -c 4 www.google.com";
        Runtime r = Runtime.getRuntime();
        Process p = r.exec(cmdPing);
        BufferedReader in = new BufferedReader( new InputStreamReader(p.getInputStream()));
        String inputLinhe;
        /**
         * Vamos pegar o resultado!
         */
        final StringBuffer buff = new StringBuffer();
        while((inputLinhe = in.readLine())!= null){
            buff.append(inputLinhe);
        }

        final Integer pacotesRecebidos = getPacotesRecebidos(buff.toString());

        Toast.makeText(getApplicationContext(), "Pacotes Recebidos: "+pacotesRecebidos, Toast.LENGTH_SHORT).show();

    }catch (final Exception e){
        e.printStackTrace();
    }


}
  • Oops, I’ll test it here.

  • didn’t work. I tried to pass pacotesrecebidos for a textview and yet nothing is displayed

  • How did it go? Can you debug or log to see?

  • W/System.err: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=19; regionLength=-20

  • See if the text has revived? Can display full text?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.