android - Sqlite database update after async post not working -
i using httppost method send message android device via web server. when response comes back, device supposed update on-board database format i've used million times,
if(postmessage(themessage).trim().equals("ok")){ databasehelper helper = new databasehelper(sharedialog.this); database = helper.getwritabledatabase(); strsharedwith = strsharedwith + thenameencoded+ "|~|";//my delimiter; string strfilter = "listtitle= '" + listtitle + "'"; contentvalues values = new contentvalues(); values.put("sharedwith", strsharedwith); int numrows = database.update("listdata", values, strfilter, null); database.close(); toast.maketext(getbasecontext(), numrows + "", toast.length_short).show();//toasts "1"
everything appears work: message sent other device, response comes web server, , toast message indicates 1 row has been updated...but value remains null. see i'm missing? thanks
edit: i've found query works fine if moved ahead of calling asynctask...seems strange me query isn't executed, know if block (because there's else block not executed). @ risk of embarrassing myself i'm posting code asynctask; know use of
while (something.equals("")){ try { thread.sleep(1000); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } }
is bad practice, way got post server work. see i'd better clean second thread usage, if (and show me update local database), i'll mark answered. oh, side note: advice use execsql generated exceptions because of improper escaping of pipe delimiter--the format i'm using avoids that. ok, red face:
if(postmessage(thenameencoded).trim().equals("ok")){ //was making database update here, didn't work m_adapter.notifydatasetinvalidated(); loadadapter(); setlistadapter(m_adapter); }else{ //alert dialog } public string postmessage(string towhom){ //get stuff database strtopost = (assembled database call); new myasynctask().execute(strtopost); while (privatestatus.equals("")){ try { thread.sleep(2000); } catch (interruptedexception e) { e.printstacktrace(); } } if (privatestatus.indexof("ok")>-1){ //the return webserver, ok on success return "ok"; }else{ return "not friend"; }
and, asynctask
private class myasynctask extends asynctask<string, integer, double>{ protected void onpreexecute() { //progress dialog started } protected double doinbackground(string... params) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://www.my-domain.com/list_post.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("listsent", strtopost)); namevaluepairs.add(new basicnamevaluepair("fromuser", myname)); namevaluepairs.add(new basicnamevaluepair("touser", sharinglistwith)); httppost.setentity(new urlencodedformentity(namevaluepairs)); status = httpclient.execute(httppost, new basicresponsehandler()); while (status.equals("")){ try { thread.sleep(1000); } catch (interruptedexception e) { e.printstacktrace(); } } setstatus(status); //exception catching protected void onpostexecute(double result){ pdialog.dismiss(); } } public string setstatus (string thestatus){ if(!thestatus.equals(null)){ privatestatus = thestatus; } return null; }
i know. and, 2 of while{sleep} things. recommendations on how incorporate response webserver in secondary thread appreciated.
i update local database calling update function before calling asynctask; used connectivitymanager (as explained in link above) prevent update occurring if httppost wasn't going happen. consider inadequate, , still don't understand why couldn't use onpostexecute section of asynctask--which, being in ui thread , being handed result web server post, seemed place this. still stumped, if can shed light on i'd appreciate it. in meantime, i'll mark answered app works!
Comments
Post a Comment