2012-04-13 1 views
1

http 포스트를 통해 내 서버에 버튼 클릭 또는 페이지 탐색과 같은 사용 로그를 보내고 있습니다. 지금은 asynctask doInBackground를 사용하고 있지만 서버가 응답 할 때까지 doInBackground 메서드가 완료되지 않습니다.응답을 기다리지 않고 사용 로그를 서버로 보내는 가장 좋은 방법은 무엇입니까

질문 : HTTP 게시물을 보내고 서버의 응답을 무시하는 것이 좋습니다. 하지만 서버가 로그를 기록하는지 확인해야합니다. 그것은 당신을 도울 수 있다면

private static class Async extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... params) { 
     String url = "server url"; 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     httpclient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password")); 
     HttpPost httppost = new HttpPost(url); 
     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("DeviceOS", "Android")); 
      nameValuePairs.add(new BasicNameValuePair("OSVersion", osVersion)); 
      nameValuePairs.add(new BasicNameValuePair("DeviceManufacturer", manufacturer)); 
      nameValuePairs.add(new BasicNameValuePair("Model", model)); 
      // and some more 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
      String responseBody = httpclient.execute(httppost, responseHandler); 

     } catch (ClientProtocolException e) { 
      // Error msg will be here = e.getMessage(); 
     } catch (IOException e) { 
      // Error msg will be here = e.getMessage(); 
     } 
     return null; 
    } 
} 

}

답변

1

이 시도 ..

Thread r = new Thread(){ 
public void run(){ 
    //post request 
} 

}.start(); 
+0

에서 호출 10 초 동안 지속됩니다. – NyanLH

+0

@ NyanLH ..하지만 걱정할 필요는 없습니다. 멀티 태스킹이 안드로이드에 좋습니다 ... 이제 요청을 보내고 떠나고 서버가 응답 할 때까지 기다리지 않겠다고 말했지 만 서버에 도달하고 보내는 중입니다. 그 데이터와 그것을 알 수있는 모든 것은 ** 전송 **의 일부입니다 스레드는 그 작업이 성공했음을 알게 될 것입니다 .. – ngesh

+0

나는 당신과 동의합니다. 그러나 기술적으로 가능하거나 그렇지 않다면 여전히 궁금합니다. :] – NyanLH

0

내가 안드로이드 함께 비동기 HTTP 작업 라이브러리를 넣어 가지고, 참조하시기 바랍니다. 당신이 asyn 할 수 HTTP 내가이 나보다 더 나은 방법이라고 생각하지만, 서버가 매우 느린 경우라고하고, 스레드를 응답 10 초 정도 걸릴합시다 활동에서 또는 IntentService

https://github.com/nareshsvs/android-libraries

관련 문제