I am sending a usage log such as button click or page navigation to my server via http post, Right now i am using the asynctask doInBackground, but my doInBackground method will not be finish until server respond.
Question is: Is there a better to just send the http post and ignore the response from server. But I need to make sure the server records the log.
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;
}
}
}
try this..