Jyosna
Jyosna

Reputation: 4446

android how to know HttpResponse takes how much time for giving response

I have an application where i want to find every time it connect with internet and fetching data from internet how much time it will take? and if takes more time then i want to give warning to user that "problem with Internet connection"

So how I can know how much time it ll take., below a function of my application I gave, where HttpResponse i used.plz tell me how to get how much time it will take to give the response

 String page = executeHttpGet("http://192.168.1.109/temp/android.php");

 private String executeHttpGet(String URL) throws Exception {

    BufferedReader bufferedReader = null;
    try {
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                "android");
        HttpGet request = new HttpGet();
        request.setHeader("Content-Type", "text/plain; charset=utf-8");
        request.setURI(new URI(URL));
        HttpResponse response = client.execute(request);
        bufferedReader = new BufferedReader(new InputStreamReader(response
                .getEntity().getContent()));

        StringBuffer stringBuffer = new StringBuffer("");
        String line = "";

        String NL = System.getProperty("line.separator");
        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line + NL);
            System.out.print(stringBuffer);
        }
        bufferedReader.close();
        page = stringBuffer.toString();
        System.out.println(page + "page");
        return page;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                Log.d("BBB", e.toString());
            }
        }
    }
}

Thank you.

Upvotes: 1

Views: 1912

Answers (2)

Siten
Siten

Reputation: 4533

Above answer is also implemented...

the simplest logic way is......

1) take the system time in some variable.

2) after response get the System time in another variable.

3) take the difference you will get approximate your response time.

Upvotes: 0

TheGuyNextDoor
TheGuyNextDoor

Reputation: 7937

..if takes more time then i want to give warning to...

Since you know the best time you should get a response why not specify a timeout when creating a connection. See the code snippet below on how to set the timeout. You then catch the timeout error and notify that the service too long to respond.

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);

ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);

Ref:

  1. http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/
  2. Http connection timeout on Android not working

Upvotes: 3

Related Questions