java - Asynchronous Task error in doInBackground method -
i have try code
public class splashscreen extends activity { string urls = "xxxxxxxxxxxxxxx"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.splash_screen); if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder() .permitall().build(); strictmode.setthreadpolicy(policy); } try { new mytask().executeonexecutor(asynctask.thread_pool_executor); } catch (exception e) { // todo: handle exception } } class mytask extends asynctask<void, void, void>{ @override protected void doinbackground(void... params) { rssfeed_saxparser saxparser = new rssfeed_saxparser(urls); log.i("clearbef", "----------------cache-clear-----------"); log.v("gettitlelist", "="+saxparser.gettitlelist().size()); log.v("getimageurllist", "="+saxparser.getimageurllist().size()); log.v("getdurationlist", "="+saxparser.getdurationlist().size()); log.v("getvideourllist", "="+saxparser.getvideourllist().size()); log.v("getbitrateurllist", "="+saxparser.getbitrateurllist().size()); log.v("getguidlist", "="+saxparser.getguidlist().size()); log.d("getpubdatelist", "="+saxparser.getpubdatelist().size()); return null; } @override protected void onpostexecute(void result) { handler handler = new handler(); handler.postdelayed(new runnable() { public void run() { finish(); intent intent = new intent(getbasecontext(), mainactivity.class); // overridependingtransition(r.anim.slide_in_left,r.anim.slide_out_right); startactivity(intent); } }, 100); super.onpostexecute(result); } } }
//--------------------------
public class rssfeed_saxparser extends activity { string streamtitle = ""; arraylist<string> mediaurllist, imageurllist, guidlist, titlelist, pubdatelist, bitratelist, durationlist; /** called when activity first created. */ public rssfeed_saxparser(string url) { mediaurllist = new arraylist<string>(); imageurllist = new arraylist<string>(); guidlist = new arraylist<string>(); titlelist = new arraylist<string>(); bitratelist = new arraylist<string>(); durationlist = new arraylist<string>(); pubdatelist = new arraylist<string>(); try { url rssurl = new url(url); saxparserfactory mysaxparserfactory = saxparserfactory .newinstance(); saxparser mysaxparser = mysaxparserfactory.newsaxparser(); xmlreader myxmlreader = mysaxparser.getxmlreader(); rsshandler myrsshandler = new rsshandler(); myxmlreader.setcontenthandler(myrsshandler); inputsource myinputsource = new inputsource(rssurl.openstream()); myxmlreader.parse(myinputsource); } catch (malformedurlexception e) { e.printstacktrace(); } catch (parserconfigurationexception e) { e.printstacktrace(); } catch (saxexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } private class rsshandler extends defaulthandler { private boolean item = false; private boolean title = false; private boolean guid = false; private boolean image = false; private boolean pubdate = false; private boolean media = false; int count = 0; long bitr = 0; string url, duration, bitrate; string imageurlvalue, guidvalue, titlevalue, pubdatevalue; private boolean flagtitle = false, flagimage = false, flagmedia = false; @override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { if (localname.equals("item")) { item = true; count++; } else if (localname.equalsignorecase("content") && item) { int w = 0; long bit = 0; try { w = integer.parseint(attributes.getvalue("width")); bit = long.parselong(attributes.getvalue("bitrate")); } catch (exception e) { e.printstacktrace(); } if (1024 <= w && bit > 1100000) { bitrate = attributes.getvalue("bitrate"); url = attributes.getvalue("url"); duration = attributes.getvalue("duration"); log.v("videowidth" + localname, "=" + attributes.getvalue("width")); flagmedia = true; }else if( w == 640 && bit > bitr && !flagmedia){ bitrate = attributes.getvalue("bitrate"); url = attributes.getvalue("url"); duration = attributes.getvalue("duration"); log.v("640videowidth" + localname, "=" + attributes.getvalue("width")); } } else if (localname.equalsignorecase("thumbnail") && item && !flagimage) { image = true; flagimage = true; log.v("imageattributes" + localname, "=" + attributes.getvalue("url")); imageurlvalue = attributes.getvalue("url"); //imageurllist.add(attributes.getvalue("url")); } else if (localname.equalsignorecase("updated") && item) { pubdate = true; } else if (localname.equalsignorecase("title") && item && !flagtitle) { title = true; } else if (localname.equalsignorecase("clipid") && item) { guid = true; } else { // log.v("=" + localname, "=" + qname); } } @override public void endelement(string uri, string localname, string qname) throws saxexception { if (localname.equals("item")) { item = false; flagimage = false; flagtitle = false; // flagmedia = false; media = true; // flagmedia = true; log.w("mediaattributes" + guidvalue, "===" + url); if(url != null) { mediaurllist.add(url); bitratelist.add(bitrate); durationlist.add((integer.parseint(duration)*1000)+""); guidlist.add("clip"+guidvalue); titlelist.add(titlevalue); pubdatelist.add(pubdatevalue); imageurllist.add(imageurlvalue); } flagmedia = false; bitr = 0; url = bitrate = duration = guidvalue = titlevalue = pubdatevalue = imageurlvalue = null; log.v("=" + count, "=" + qname); } } @override public void characters(char[] ch, int start, int length) throws saxexception { if (title && item) { title = false; flagtitle = true; log.d("title=", "=" + new string(ch, start, length)); //titlelist.add(new string(ch, start, length)); titlevalue = new string(ch, start, length); } else if (guid && item) { guid = false; //guidlist.add(new string(ch, start, length)); log.d("guid=", "=" + new string(ch, start, length)); guidvalue = new string(ch, start, length); } else if (image && item) { image = false; // log.d("image=", "=" + new string(ch, start, length)); } else if (pubdate && item) { pubdate = false; //pubdatelist.add(new string(ch, start, length)); pubdatevalue = new string(ch, start, length); log.d("pubdate=", "=" + new string(ch, start, length)); } else if (media && item) { media = false; // log.d("media=", "=" + new string(ch, start, length)); } } } public arraylist<string> gettitlelist() { return titlelist; } public arraylist<string> getpubdatelist() { return pubdatelist; } public arraylist<string> getimageurllist() { return imageurllist; } public arraylist<string> getguidlist() { return guidlist; } public arraylist<string> getdurationlist() { return durationlist; } public arraylist<string> getbitrateurllist() { return bitratelist; } public arraylist<string> getvideourllist() { return mediaurllist; } }
please let me know doing wrong in advance.
error getting below
06-18 07:14:31.662: e/androidruntime(16819): fatal exception: asynctask #1 06-18 07:14:31.662: e/androidruntime(16819): java.lang.runtimeexception: error occured while executing doinbackground() 06-18 07:14:31.662: e/androidruntime(16819): @ android.os.asynctask$3.done(asynctask.java:299) 06-18 07:14:31.662: e/androidruntime(16819): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 06-18 07:14:31.662: e/androidruntime(16819): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 06-18 07:14:31.662: e/androidruntime(16819): @ java.util.concurrent.futuretask.run(futuretask.java:239) 06-18 07:14:31.662: e/androidruntime(16819): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 06-18 07:14:31.662: e/androidruntime(16819): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 06-18 07:14:31.662: e/androidruntime(16819): @ java.lang.thread.run(thread.java:856) 06-18 07:14:31.662: e/androidruntime(16819): caused by: java.lang.runtimeexception: can't create handler inside thread has not called looper.prepare() 06-18 07:14:31.662: e/androidruntime(16819): @ android.os.handler.<init>(handler.java:197) 06-18 07:14:31.662: e/androidruntime(16819): @ android.os.handler.<init>(handler.java:111) 06-18 07:14:31.662: e/androidruntime(16819): @ android.app.activity.<init>(activity.java:780) 06-18 07:14:31.662: e/androidruntime(16819): @ com.fusioni.xmlparser.rssfeed_saxparser.<init>(rssfeed_saxparser.java:27) 06-18 07:14:31.662: e/androidruntime(16819): @ com.fusioni.spacealabama.splashscreen$mytask.doinbackground(splashscreen.java:111) 06-18 07:14:31.662: e/androidruntime(16819): @ com.fusioni.spacealabama.splashscreen$mytask.doinbackground(splashscreen.java:1) 06-18 07:14:31.662: e/androidruntime(16819): @ android.os.asynctask$2.call(asynctask.java:287) 06-18 07:14:31.662: e/androidruntime(16819): @ java.util.concurrent.futuretask.run(futuretask.java:234) 06-18 07:14:31.662: e/androidruntime(16819): ... 3 more
two major things wrong:
you cannot call
new
onactivity
. created on demand system based onintent
objects.you cannot create
handler
objects (by way ofnew rsshandler()
call) inside ofasynctask
. temporary, short lived background processor run in arbitrary thread.handler
requireslooper
has been started , boundthread
's running context.
Comments
Post a Comment