Android 异步任务

发布于 2024-10-31 09:13:57 字数 2023 浏览 2 评论 0原文

我有一个应用程序,它获取特定日期的数据作为网络服务器的响应,并根据收到的数据(数量/时间)绘制 O/P 图。

现在我正在将网络服务器作为异步任务调用。

我还可以跨多个屏幕(前一天/后一天)多次调用 Web 服务器(多个异步任务),以便从服务器获取数据。

问题是当 flings(异步任务)数量增加时,应用程序会显示 ANR。

有没有更好的方法来处理此类场景,而不是为每个 Web 服务器调用(fling)创建异步任务。

添加部分代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事风中埋 2024-11-07 09:13:57

只要您在主线程上创建 AsyncTask 对象并从主线程调用它们的执行,就不应该有任何问题。您可以为您的 AsyncTask 发布一些伪代码吗?
您可以在此处找到有关 AsyncTask 的更多信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文