I am using AsyncTask to perform an HTTP GET on android. It causes a long delay in sending my data to the URL. Is there a way to fix this or another approach that can be used?
Here is my code: class RequestTask extends AsyncTask{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
HttpParams httpParameters = httpclient.getParams();
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
}
}
And here is the call:
new RequestTask().execute("http://10.10.10.211:9081/?modelname=model1&sourcedata=" + values[1]);