Question

I have an app which sends an HTTP request every 0.5 seconds using a Timer. I have it in asyncTask and I read data from output after it's done from onPostExecute. After a while my app starts lagging and the UI slows down. Do you know why it happens and how to do this right ?

public class Komunikace extends AsyncTask<String, Void, String> {
     public static KomunikaceInterface delegate=null;
    //ProgressDialog progress;
String response = "";
String url = "";
DefaultHttpClient client;
HttpGet httpGet;
HttpResponse execute;
InputStream content;
BufferedReader buffer;
String s = "";

    protected String doInBackground(String... params)
    {
        url = params[0];

        client = new DefaultHttpClient();
        httpGet  = new HttpGet(url);
            try {
                execute = client.execute(httpGet);
                content = execute.getEntity().getContent();

                buffer = new BufferedReader(new InputStreamReader(content));

                while ((s = buffer.readLine()) != null) {
                    response += s + "\n";
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        client.getConnectionManager().shutdown();
        return response;

    }

    protected void onPreExecute()
    {
        super.onPreExecute();
    }

    protected void onPostExecute(String result)
    {
        delegate.processFinish(result); // prijata tabulka
    }
    }

This is my asyncTask class. Then I call it like this:

    tajmr = new Timer();
    tajmr.schedule(new TimerTask() {
        @Override
        public void run() {
            CallWebService();
        }

    }, 0, 100);

And then this for the call:

    private void CallWebService()
{
    this.runOnUiThread(fetchData);
}
private Runnable fetchData = new Runnable() {
    public void run() {
        try
        {
            updateURL =url;
            Komunikace komunikace= new Komunikace();
            komunikace.execute(updateURL);
        }
        catch (Exception e)
        {

        }
    }
};

Looks like it's looping somewhere and after a while it's slowing down. This while takes for example 1 minute.

Was it helpful?

Solution

If you're timer ticks every 0.5 seconds and the http request takes 1 second, after a while your have dozens of http requests waiting in the queue and using memory.

Additionally your logic might be too complex. The timer has it's own thread, in that thread you ask to do some job on the UI thread, and that job consists in creating an AsyncTask??? In the end, it could be possible that all AsyncTask as running concurrently, and you have many HTTP concurrent requests.

Why don't you just make the HTTP request in the timer thread or the AsyncTask?

EDIT:
What you could do is starting the timer count each time the previous http request has finished. You could do this with a worker thread (instead of a timer) with an infinite loop (with some exit condition) that makes the request, and when it finishes it sleeps for 0.5 seconds. This way you ensure you always have one single http request at any time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top