java - How can I set doInbackground finish its task before continuing the mainActivity? -
these mainactivity
:
database_connector wp_terms = new database_connector("select * `dse120071750`.`wp_terms` ",progressdialog,this); wp_terms.execute(); wp_terms.onpreexecute(); try { (int i=0; i<wp_terms.getjsonarray().length(); i++){ jsonobject obj = wp_terms.getjsonarray().getjsonobject(i); this.wp_terms.put(obj.getstring("term_id"), obj.getstring("name")); } } catch (jsonexception e) { e.printstacktrace(); }
databaseconnector:
package hk.hoome.www.mobilehoome; public class database_connector extends asynctask<void,void, void> { //string mode; httpresponse response; string sql; jsonarray jsonarray; searchpage searchpage; public database_connector(string sql, searchpage searchpage){ //this.mode = mode; this.sql = sql; this.searchpage = searchpage; jsonarray = new jsonarray(); } @override protected void doinbackground(void... params) { connect(); publishprogress(); return null; } @override protected void onprogressupdate(void... values) { } public void connect() { list<namevaluepair> namevaluepair = new arraylist<namevaluepair>(1); namevaluepair.add(new basicnamevaluepair("sql", sql)); //namevaluepair.add(new basicnamevaluepair("mode", mode)); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.hoome.hk/hoomemobileapps/connectdb.php"); httppost.setentity(new urlencodedformentity(namevaluepair, "utf-8")); response = httpclient.execute(httppost); httpentity httpentity = response.getentity(); string entityresponse = entityutils.tostring(httpentity); log.e("entity response ", entityresponse.substring(2)); jsonarray = new jsonarray(entityresponse.substring(2)); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } } public jsonarray getjsonarray(){ return jsonarray; }
}
when ran code, for (int i=0; i<wp_terms.getjsonarray().length(); i++){
results in nullpointerexception
.
i believe because doinbackground
hasn't finished process mainactivity
keeps running. how can set doinbackground
has done before continue running mainactivity?
solution?
try { while (wp_posts.getjsonarray().equals(null)) thread.sleep(1000); } catch(interruptedexception ex) { thread.currentthread().interrupt(); }
is solution?
use callback in database connector class , pass completion callback. simplicity i'm using runnable interface. in general, try have own interface , pass params between background thread , main thread on custom interface.
package hk.hoome.www.mobilehoome; public class database_connector extends asynctask<void,void, void> { //string mode; httpresponse response; string sql; jsonarray jsonarray; searchpage searchpage; private runnable activitycallback; public void setcallback(runnable callback) { this.activitycallback = callback; } public database_connector(string sql, searchpage searchpage){ //this.mode = mode; this.sql = sql; this.searchpage = searchpage; jsonarray = new jsonarray(); } @override protected void doinbackground(void... params) { connect(); publishprogress(); return null; } protected void onpostexecute(void result) { if(activitycallback != null) { activitycallback.run(); } } @override protected void onprogressupdate(void... values) { } public void connect() { list<namevaluepair> namevaluepair = new arraylist<namevaluepair>(1); namevaluepair.add(new basicnamevaluepair("sql", sql)); //namevaluepair.add(new basicnamevaluepair("mode", mode)); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.hoome.hk/hoomemobileapps/connectdb.php"); httppost.setentity(new urlencodedformentity(namevaluepair, "utf-8")); response = httpclient.execute(httppost); httpentity httpentity = response.getentity(); string entityresponse = entityutils.tostring(httpentity); log.e("entity response ", entityresponse.substring(2)); jsonarray = new jsonarray(entityresponse.substring(2)); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (jsonexception e) { e.printstacktrace(); } } public jsonarray getjsonarray(){ return jsonarray; } }
in mainactivity
database_connector wp_terms = new database_connector("select * `dse120071750`.`wp_terms` ",progressdialog,this); wp_terms.setcallback(new runnable() { public void run() { try { (int i=0; i<wp_terms.getjsonarray().length(); i++){ jsonobject obj = wp_terms.getjsonarray().getjsonobject(i); this.wp_terms.put(obj.getstring("term_id"), obj.getstring("name")); } } catch (jsonexception e) { e.printstacktrace(); } } }); wp_terms.execute();
Comments
Post a Comment