java - AsyncTask/Handler lagging UI -
so have asynctask gets data website, , on it's post execute calls main function settext main's textview.
here code.
@override protected void doinbackground(string... arg0) { result = connect(start);//connect webpage, start 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 textview }
i call handler every 5 seconds, here 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 website loading, ui lags lot, point of freezing. have no idea causing this, ui based on viewpager, fragments.
i not running code fragment though, running oncreate of main activity.
edit: edited onpostexecute 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 = 0 ; < 3 ; ++) { arr[i] = stuff.get(79 + i).text(); } mainactivity.getdata(arr); mainactivity.dismissloading(); }
this getdata
public static void getdata(string[] s) { edit.settext(s[0]); }
this 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 stumbled upon problem , wanted answer, managed fix while working on project.
basically, in
while ((result = br.readline()) != null) { result2 += result; }
loop, instead of appending result result2 directly, append result stringbuffer. @ end of loop, add stringbuffer result2.
Comments
Post a Comment