1

I have an application which is getting the data for a particular day as a response from a web-server and plotting the o/p graph depending on the data(Number/Time) which has come.

Right now i am invoking the web-server as an async task.

I can also fling across multiple screens(previous day/next day) to invoke web-server multiple times(multiple async tasks) so as to get the data from the server.

The issue is when the number of flings(async tasks) is increasing the application shows ANR.

Is there any better way to handle these kind of scenarios rather then creating async task for each and every web-server call(fling).

Adding portion of code:

if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE 
          && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
      valuesDayWithTime=onStart(datePrevious);
}



public GraphView onStart(String date){
    urlFinal=createUrl();
    new DownloadDataTask(this).execute(urlFinal);
}

private class DownloadDataTask extends AsyncTask<String, Integer, Long> {

    YieldActivity yActivity;
    ProgressBar pBar ;
    DownloadDataTask(YieldActivity act){
        yActivity=act;
    }

    protected void onPreExecute() {
        relLay=(RelativeLayout) findViewById(R.id.graphView);
        pBar
        = new ProgressBar(yActivity);
        LayoutParams lp =new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
        pBar.setLayoutParams(lp);
        pBar.setMax(100);
        relLay.removeAllViews();
        relLay.addView(pBar);
    }

    protected Long doInBackground(String... urls) {
        int totalCount = urls.length;
        for (int i = 0; i < totalCount; i++) {
            publishProgress((int) ((i / (float) totalCount) * 100));
            downloadFromUrl(urls[i]);
        }
        return (long) totalCount;
    }

    protected void onProgressUpdate(Integer... progress) {
        Log.i(progress[0] +"%");
    }

    protected void onPostExecute(Long result) {

        graphViewDay=calculate(dateNow,valuesDayWithTime);
        relLay.removeView(pBar);
        relLay.addView(graphViewDay);


    }
}

public void downloadFromUrl(String fromURL) {
    try {
        getHttpResponse(fromURL.toString());

        parseResponse();
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
}
1
  • Please post the solution that worked for you so others can refer. Commented Apr 21, 2011 at 16:02

1 Answer 1

2

As long as you are creating the AsyncTask objects on the main thread and calling execute on them from the main thread, there shouldn't be any problem. Can you post some pseudo code for your AsyncTask ? You can find more on AsyncTask here.

2
  • hi advantej,i have added some bit of code snippet which i am doing for this.
    – Deva
    Commented Apr 11, 2011 at 19:32
  • Are you sure that none of the methods of DownloadTask that run on the main thread (onPreExecute, onPostExecute and onProgressUpdate) are taking long to execute ? That is the only reason I think an ANR will be thrown. Any long task in the doInBackground is acceptable.
    – advantej
    Commented Apr 11, 2011 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.