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();
}
}