2015-01-05 3 views
0

게시물 요청이 얼마나 빨리 실행되었는지 확인하려고합니다. AsyncTask 내에서 요청을 수행하고 있습니다. 나는 현재 실행 시간안드로이드 게시물 요청이 얼마나 빨리 걸리는지 확인하십시오.

  long start = System.nanoTime(); 
      new POSTTask().execute(); 
      long end = System.nanoTime() - start; 

를 확인하고이에 의해 주어진 시간은 POST 요청 걸리는 실제 시간이면 나는 확실하지 않다 방법 이것은

. AsyncTask 내부에서 내가하는 일은 다음과 같습니다.

 try { 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost request = new HttpPost(url); 

      List<NameValuePair> postParameters = new ArrayList<>(); 
      postParameters.add(new BasicNameValuePair("test", "tets1")); 

      UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 

      request.setEntity(formEntity); 
      HttpResponse response = httpClient.execute(request); 
      result = EntityUtils.toString(response.getEntity()); 

      Log.i("Result", result); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

나는 올바른 방향으로 또는 잘못된 방향으로 가고 있습니까?

+0

에 모든 타이밍을 일을해야 당신을 AsyncTask에서'async'가 의미하는 바는 무엇입니까? – njzk2

답변

0

아니요, 실제 시간이 아닙니다. 주 스레드에서 실행 시간을 얻으려고하고 있지만 게시 쿼리가 백그라운드의 다른 스레드에서 실행 중입니다.

이 잘못이

try { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost request = new HttpPost(url); 

     List<NameValuePair> postParameters = new ArrayList<>(); 
     postParameters.add(new BasicNameValuePair("test", "tets1")); 

     UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters); 

     request.setEntity(formEntity); 
     long start = System.nanoTime(); 
     HttpResponse response = httpClient.execute(request); 
     long end = System.nanoTime() - start; 
     // Print value of end here 
     result = EntityUtils.toString(response.getEntity()); 

     Log.i("Result", result); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

완벽합니다. 8 분 후 바로 표시됩니다. –

+0

기꺼이 도와 드리겠습니다. 고맙습니다!! :디 – Rohit5k2

0

아니처럼 사용합니다. AsyncTaskexecute 메서드는 요청이 수행되기 전에 즉시 반환됩니다.

당신은 특별히

 HttpResponse response = httpClient.execute(request); 

당신이 실제 시간을 얻을 것이다이 라인 주위를 넣어의 AsyncTask에 타이밍 코드를 이동합니다.

0

해당 행이 순서대로 실행되지 않으므로 잘못되었습니다.

new POSTTask().execute(); 

은 2 개 시간 스탬프가 정말 떨어져 밀리 초처럼 함께 종료 될 것하고 먹으 렴하는 의미를 새 스레드를 시작 정확하지

당신이 당신의 doInBackground

관련 문제