简体   繁体   中英

AsyncTask/Handler lagging UI

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM