android - AsyncTask in onCreate method -
i have simple application plays online radio. showing title online php service use asynctask , call oncreate method. in android 4 everythin ok, in android 2 it's crushed error
java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare()
then in internet found, must use code
new thread(new runnable() { @override public void run() { mainactivity.this.runonuithread(new runnable() { @override public void run() { //my code } }); } }).start();
but after using tip, can't see button , text views in android 4 , android 2 versions. code:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //thread update title every second new thread(new runnable() { @override public void run() { mainactivity.this.runonuithread(new runnable() { @override public void run() { while(true) { try { new showtitle() .execute("http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm"); thread.sleep(1000); } catch (interruptedexception e) { string tag = "update title"; log.e(tag, "update title crashed", e); } } } }); } }).start(); } //get title string online source private string getmusictitle(string url) { document doc = null; string title = "music title"; try { url = "http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm"; inputstream input = new url(url).openstream(); doc = jsoup.parse(input, "cp1251", url); title = doc.body().text();//doc.select(".products_name").first().text(); } catch (ioexception e) { log.e(tag, "failed load html code", e); toast.maketext(this, "failed load title", toast.length_short).show(); } return title; } //class show audio title private class showtitle extends asynctask<string, void, string> { @override protected string doinbackground(string... urls) { return getmusictitle(urls[0]); } protected void onpostexecute(final string result) { lblmusicname.settext(result); } }
edit: (my working code)
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new showtitle() .execute("http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm"); } private string getmusictitle(string url) { document doc = null; string title = "music title"; try { url = "http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm"; inputstream input = new url(url).openstream(); doc = jsoup.parse(input, "cp1251", url); title = doc.body().text(); } catch (ioexception e) { log.e(tag, "failed load html code", e); title = "failed load title"; } return title; } private class showtitle extends asynctask<string, string, string> { @override protected string doinbackground(string... urls) { while (true) { string str = getmusictitle(urls[0]); publishprogress(str); try { thread.sleep(1000); } catch (interruptedexception e) { string tag = "update title"; log.e(tag, "update title crashed", e); } } } protected void onprogressupdate(string... result) { lblmusicname.settext(result[0]); } }
runonuithread
, asynctask
2 different things. using in wrong way.
try this:-
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); new showtitle().execute("http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm"); } //get title string online source private string getmusictitle(string url) { document doc = null; string title = "music title"; try { url = "http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm"; inputstream input = new url(url).openstream(); doc = jsoup.parse(input, "cp1251", url); title = doc.body().text();//doc.select(".products_name").first().text(); } catch (ioexception e) { log.e(tag, "failed load html code", e); title = "failed load title"; } return title; } //class show audio title private class showtitle extends asynctask<string, string, string> { @override protected string doinbackground(string... urls) { string str = getmusictitle(urls[0]); while(true) { publishprogress(str); try { thread.sleep(1000); } catch (interruptedexception e) { string tag = "update title"; log.e(tag, "update title crashed", e); } } return str; } @override protected void onprogressupdate(string... progress) { if(returnval.startswith("failed")) { toast.maketext(this, returnval, toast.length_short).show(); } else { lblmusicname.settext(result); } } }
you must ui related task in onprogressupdate
Comments
Post a Comment