Know the URL read time

0

I'm reading content that has a url

Using the following code block:

String urlNormal = "http://minhaurl";
URLConnection conn = urlNormal.openConnection();
BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()));

I would like to know if I have to know how long it will take for him to read the data or how much time he spent trying to read

    
asked by anonymous 20.06.2018 / 16:55

2 answers

2

It could look like this:

long inicio = System.currentTimeMillis();

String urlNormal = "http://minhaurl";
URLConnection conn = urlNormal.openConnection();
BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream()));

long fim = System.currentTimeMillis();
System.out.println("Tempo: " + (fim - inicio));

Give you time in ms.

    
20.06.2018 / 17:05
2

Here is an example below:

    //Registrar tempo inicial:
    long time_begin = System.currentTimeMillis();

    //Declarar URL, e criar objeto do tipo URL:
    String example_your_URL = "https://jsonplaceholder.typicode.com/posts/1";
    URL url = new URL(example_your_URL);

    //Abrir conexão:
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));

    //Registrar tempo final:
    long time_end = System.currentTimeMillis();

    //Tempo gasto:
    double milliseconds = (time_end - time_begin);

    //Imprimir tempo gasto:
    System.out.println("Tempo em milissegundos: " + milliseconds+"\n");

    //Ler conteúdo:
    String content = null;
    while( (content = br.readLine()) != null) {
        System.out.println(content);
    }
    
20.06.2018 / 17:32