AsyncTask的/处理器滞后UI [英] AsyncTask/Handler lagging UI

查看:79
本文介绍了AsyncTask的/处理器滞后UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这样的AsyncTask从网站获取数据,并在它的后执行它调用一个主要功能的setText主的TextView的。

下面是code。

  @覆盖
保护无效doInBackground(字符串...为arg0){
    结果=连接(开始); //连接到网页,开始是一个URL
    // TODO自动生成方法存根
    返回null;
}@覆盖
保护无效onPostExecute(虚空结果){
    super.onPostExecute(结果);    文档的DOC = Jsoup.parse(this.result);
    元素的东西= doc.select(TD);    MainActivity.GetData(DOC); //设置的TextView
}

我所说的处理要做到这一点每五秒钟,这里是处理code。

 手=新的处理程序();
R =新的Runnable(){    @覆盖
    公共无效的run(){
            DH =新DownloadHelper(\"http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours\");
            dh.execute(); // TODO自动生成方法存根
            hand.postDelayed(这一点,10000);
        }
    };hand.post(R);

什么情况是,该网站加载的时候,我的UI滞后了很多,几乎冻结的地步。我不知道是什么原因造成这一点,我的UI是基于ViewPager,用碎片。

我不是从片段运行此code,虽然,它是从我的主要活动的onCreate运行。

编辑:我编辑我onPostExecute看起来像这样

  @覆盖
保护无效onPostExecute(虚空结果){
    // TODO自动生成方法存根    super.onPostExecute(结果);        元素的东西= doc.select(TD);
    的String [] =改编新的String [stuff.size()];
    的for(int i = 0;我3;;我++)
    {        改编[I] = stuff.get(79 + I)的.text();    }    MainActivity.GetData(ARR);
    MainActivity.dismissLoading();
}

这是我的GetData

 公共静态无效的GetData(字符串[] S)
{ edit.setText(S [0]);
}

这是连接()

 公共静态字符串连接(字符串升)
{    字符串URL = 1;
    HttpURLConnection的连接;
    字符串结果;
    字符串结果2 = NULL;
    BR的BufferedReader;    尝试{
        连接=(HttpURLConnection类)(新的URL(网址))的openConnection()。
        BR =新的BufferedReader(新的InputStreamReader(connect.getInputStream()));
        而((结果= br.readLine())!= NULL)
        {
             结果2 + =结果;
        }
    }赶上(例外五){
        // TODO自动生成catch块            e.printStackTrace();
            System.out.print(错误);
    }
    返回结果2;}


解决方案

有关的人究竟是谁偶然发现了我的问题,想要一个答案,我设法解决它的另一个项目时。

基本上,在

 ,而((结果= br.readLine())!= NULL)
    {
         结果2 + =结果;
    }

循环,而不是附加的结果直接RESULT2,追加结果一个StringBuffer。然后在循环结束时,StringBuffer的添加回RESULT2

So I have this AsyncTask that gets data from a website, and on it's post execute it calls a main function to setText for main's textview.

Here is the code.

@Override
protected Void doInBackground(String... arg0) {     
    result = connect(start);//connect to the webpage, start is a URL
    // TODO Auto-generated method stub
    return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);

    Document doc = Jsoup.parse(this.result);
    Elements stuff = doc.select("td");

    MainActivity.GetData(doc);//set the textview
}

I call a handler to do this every five seconds, here is the handler code.

hand = new Handler();       
r = new Runnable() {

    @Override
    public void run() {
            dh  = new DownloadHelper("http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours");
            dh.execute("");// TODO Auto-generated method stub
            hand.postDelayed(this, 10000);
        }
    };

hand.post(r);

What happens is, when the website is loading, my UI lags a lot, almost to the point of freezing. I have no idea what is causing this, my UI is based on a ViewPager, with fragments.

I am not running this code from the fragment though, it is running from the onCreate of my main activity.

EDIT: I edited my onPostExecute to look like this

@Override
protected void onPostExecute(Void result) {
    // TODO Auto-generated method stub



    super.onPostExecute(result);

        Elements stuff = doc.select("td");
    String[] arr = new String[stuff.size()];
    for(int i = 0 ; i < 3 ; i ++)
    {

        arr[i] = stuff.get(79 + i).text();

    }

    MainActivity.GetData(arr);
    MainActivity.dismissLoading();
}

This is my GetData

public static void GetData(String[] s)
{

 edit.setText(s[0]);




}

This is connect()

public static String connect(String l)
{

    String url = l;
    HttpURLConnection connect;
    String result;
    String result2 = null;
    BufferedReader br;

    try {
        connect = (HttpURLConnection) (new URL(url)).openConnection();
        br = new BufferedReader(new InputStreamReader(connect.getInputStream()));
        while ((result = br.readLine()) != null)
        {
             result2 += result;
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block

            e.printStackTrace();
            System.out.print("ERROR");
    }
    return result2;

}

解决方案

For anyone who actually stumbled upon my problem and wanted an answer, I managed to fix it while working on another project.

Basically, in the

   while ((result = br.readLine()) != null)
    {
         result2 += result;
    }

loop, instead of appending result to result2 directly, append result to a StringBuffer. Then at the end of the loop, add back the StringBuffer to result2.

这篇关于AsyncTask的/处理器滞后UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆