android - How to run this code in a Thread or Async Task? -


when tried running code error, strictmode thread policy, added line.

strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build();      strictmode.setthreadpolicy(policy);   

i later discovered not best approach have run in thread or ansync task, run in thread or asynctask. please edit code when answering

@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // requestwindowfeature(window.feature_no_title); // getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, // windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_live_streaming);  actionbar actionbar = getsupportactionbar(); actionbar.setdisplayhomeasupenabled(true); abm = new actionbarmenu(livestreaming.this);  strictmode.threadpolicy policy = new strictmode.threadpolicy.builder()         .permitall().build(); strictmode.setthreadpolicy(policy);  if (internetstatus.getinstance(this).isonline(this)) {      xmlparser parser = new xmlparser();     string xml = parser.getxmlfromurl(url); // getting xml url     document doc = parser.getdomelement(xml); // getting dom element      // nodelist nl = doc.getelementsbytagname();      nodelist studentlist = doc.getelementsbytagname("streamingurl");      // print total student elements in document     // system.out.println("total students: " + studentlist.getlength());     toast.maketext(getbasecontext(), "please wait while stream loads",             toast.length_short).show();      if (studentlist != null && studentlist.getlength() > 0) {         (int = 0; < studentlist.getlength(); i++) {              node node = studentlist.item(i);              if (node.getnodetype() == node.element_node) {                  element e = (element) node;     nodelist nodelist = e.getelementsbytagname("andhigh_value");         theandroid_highvalue = nodelist.item(0).getchildnodes()                         .item(0).getnodevalue();              }              vid = (videoview) findviewbyid(r.id.videoview);             vid.setvideopath(theandroid_highvalue);         // static final string key_ipadhigh = "ipadhigh_value";         mediacontroller mediacontroller = new mediacontroller(this);             mediacontroller.setanchorview(vid);             // vid.setmediacontroller(mediacontroller);             vid.requestfocus();             vid.start();          videobuffering = new progressdialog(livestreaming.this);             videobuffering.setmessage("loading...please wait");             // videobuffering.seticon(r.drawable.ic_launcher);             // videobuffering.settitle(r.string.app_name);         videobuffering.setprogressstyle(progressdialog.style_spinner);             videobuffering.show();      vid.setonerrorlistener(new onerrorlistener() {     @override     public boolean onerror(mediaplayer mp, int what,             int extra) {             // log.e(tag, "error playing video");  // toast.maketext(getbasecontext(),"no stream found",toast.length_short).show();  alertdialog netalert = new alertdialog.builder(livestreaming.this).create();  netalert.setmessage("no stream found!");         netalert.setbutton("ok",         new dialoginterface.onclicklistener() {         public void onclick(     dialoginterface dialog,int which) {              // here can add functions             // finish();                     }                     });                     netalert.show();                     return true;                 }             });              vid.setonpreparedlistener(new onpreparedlistener() {                 @override                 public void onprepared(mediaplayer mp) {                      videobuffering.cancel();                 }             });          }     } else {         toast.maketext(getbasecontext(), "no internet connection",                 toast.length_short).show();         // toast t =         // toast.maketext(this,"you not online!!!!",8000).show();         log.v("home",             "############################you not online!!!!");     }  }   // liveblog_tab = (linearlayout) findviewbyid(r.id.liveblog_tab);  addcom_tab = (imageview) findviewbyid(r.id.addcom);   liveblog = (imageview) findviewbyid(r.id.addfull);  // comments = (linearlayout) findviewbyid(r.id.comments);   flag = live_blog;   ourbrow = (webview) findviewbyid(r.id.browser);  // adding webviewclient prevents web-view launching every-time  // web-site visited  ourbrow.setwebviewclient(new webviewclient());  // ourbrow.getsettings().setbuiltinzoomcontrols(true);  // ourbrow.getsettings().setsupportzoom(true);  ourbrow.getsettings().setjavascriptenabled(true);  ourbrow.getsettings().setallowfileaccess(true);  ourbrow.loadurl("http://");  ourbrow.setwebviewclient(new webviewclient() {     public void onreceivederror(webview view, int errorcode,             string description, string failingurl) {         ourbrow.loadurl("file:///android_asset/internet.htm");     }  });   addcom_tab.setonclicklistener(new onclicklistener() {     @override     public void onclick(view arg0) {         // flag = comment;         // changeview();         viewcategory();      }  });   liveblog.setonclicklistener(new onclicklistener() {      public void onclick(view v) {  intent = new intent(livestreaming.this, fullvideoview.class);         i.putextra("video_url", "theandroid_highvalue");         startactivity(i);     } });  } 

you can try below.

button1.setonclicklistener(new onclicklistener() {             @override             public void onclick(view arg0) {                 new asyn().execute();             }         }); 

then create asynctask this.

class asyn extends asynctask<string, string, string> {          @override         protected void onpreexecute() {             super.onpreexecute();             pdialog = new progressdialog(youractivityname.this);             pdialog.setmessage("loading... please wait...");             pdialog.setcancelable(false);             pdialog.show();         }          protected string doinbackground(string... args) {         if (internetstatus.getinstance(this).isonline(this)) {  xmlparser parser = new xmlparser(); string xml = parser.getxmlfromurl(url); // getting xml url document doc = parser.getdomelement(xml); // getting dom element  // nodelist nl = doc.getelementsbytagname();  nodelist studentlist = doc.getelementsbytagname("streamingurl");  // print total student elements in document // system.out.println("total students: " + studentlist.getlength()); toast.maketext(getbasecontext(), "please wait while stream loads",         toast.length_short).show();  if (studentlist != null && studentlist.getlength() > 0) {     (int = 0; < studentlist.getlength(); i++) {          node node = studentlist.item(i);          if (node.getnodetype() == node.element_node) {              element e = (element) node; nodelist nodelist = e.getelementsbytagname("andhigh_value");     theandroid_highvalue = nodelist.item(0).getchildnodes()                     .item(0).getnodevalue();          }          vid = (videoview) findviewbyid(r.id.videoview);         vid.setvideopath(theandroid_highvalue);     // static final string key_ipadhigh = "ipadhigh_value";     mediacontroller mediacontroller = new mediacontroller(this);         mediacontroller.setanchorview(vid);         // vid.setmediacontroller(mediacontroller);         vid.requestfocus();         vid.start();      videobuffering = new progressdialog(livestreaming.this);         videobuffering.setmessage("loading...please wait");         // videobuffering.seticon(r.drawable.ic_launcher);         // videobuffering.settitle(r.string.app_name);     videobuffering.setprogressstyle(progressdialog.style_spinner);         videobuffering.show();  vid.setonerrorlistener(new onerrorlistener() { @override public boolean onerror(mediaplayer mp, int what,         int extra) {         // log.e(tag, "error playing video");      // toast.maketext(getbasecontext(),"no stream found",toast.length_short).show();  alertdialog netalert = new alertdialog.builder(livestreaming.this).create();  netalert.setmessage("no stream found!");         netalert.setbutton("ok",         new dialoginterface.onclicklistener() {         public void onclick(     dialoginterface dialog,int which) {         // here can add functions         // finish();                 }                 });                 netalert.show();                 return true;             }         });          vid.setonpreparedlistener(new onpreparedlistener() {             @override             public void onprepared(mediaplayer mp) {                  videobuffering.cancel();             }         });      } } else {     toast.maketext(getbasecontext(), "no internet connection",             toast.length_short).show();     // toast t =     // toast.maketext(this,"you not online!!!!",8000).show();     log.v("home",         "############################you not online!!!!"); } }             return null;         }          protected void onpostexecute(string file_url) {             pdialog.dismiss();         }     } 

here pdialog progressdialog , can define progressdialog pdialog; or if don't want fore event on button;s click event can directly use line in oncreate method new asyn().execute();.

edit

i have edited answer, have make necessary changes in this, think ask add try , catch block somewhere, add it.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -