0

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]);

Amanda Watson
  • 340
  • 2
  • 3
  • 14
  • `causes a long delay` define that, and explain a/ how you observe that and b/ what makes you think it is related to the use of an asynctask – njzk2 Jul 07 '15 at 17:50
  • Is it possible that your task isn't actually running asynchrounously? Try running it on the executor via task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); You may even want to implement something like http://stackoverflow.com/a/12160159/1426565 to help with other tasks you want to run independently – Cruceo Jul 07 '15 at 17:51
  • By long delay, I mean that it takes 9-10 seconds to get a response when using my android device. When I run the same request on my computer using a java application the response is almost instantaneous. – Amanda Watson Jul 07 '15 at 18:29

3 Answers3

0

It's not necessarily an answer to your issue but I'd really recommend use of Retrofit for making REST calls from Android (and even better along with RxJava). In either case you shouldn't see much delay from fact that request is being made in background thread.

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
0

I cannot think of any reason why there would be delay besides any network issue!

Handling Http network calls are a pain and a a lot of work needs to be done to handle it, however, why reinvent the wheel?!

There are libraries out there that does takes care of all the messy stuff for us, use one of them.

I use OKHTTP in my project and it's pretty good.

This is an answer by CommonsWare,

Please go read it, it would prove very helpful.

Community
  • 1
  • 1
rgv
  • 1,186
  • 1
  • 17
  • 39
0

I recommend you to use Volley for this approach. I use it in my projects and I've never gotten any problem about that.

Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51