android - Some data missing when being inserted into MySQL -
like many, new android , java programming. working on project employer scan labels , compare serials match , upload database. have mysql database set @ home , inserting data expected of 'contlabel' scans missing in table though in edittext boxes. please excuse cobbled code, still learning quite bit , doing test. can please explain why of data missing though there data in required fields please? thank in advance.
this jsonparser:
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; // constructor public jsonparser() { } // function json url // making http post or mehtod public jsonobject makehttprequest(string url, string method, list<namevaluepair> params) { // making http request try { // check request method if(method == "post"){ // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httppost.setentity(new urlencodedformentity(params, "utf-8")); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); = httpentity.getcontent();
and here activity async:
// called when compare button clicked public void compareandinsert(view view) { hashmap<string, string> queryvalues = new hashmap<string, string>(); queryvalues.put("pickticket", getpickticket.gettext().tostring()); queryvalues.put("userid", getuserid.gettext().tostring()); queryvalues.put("contlabel", getcontlabel.gettext().tostring()); queryvalues.put("qrlabel", getqrlabel.gettext().tostring()); string cl = getcontlabel.gettext().tostring(); string ql = getqrlabel.gettext().tostring(); string subclabel = cl.substring(3); string subqlabel = ql.substring(3); if (subclabel.equals(subqlabel) && trigger == true) { toast toast = new toast(this); imageview view1 = new imageview(this); view1.setimageresource(r.drawable.pass); toast.setview(view1); toast.show(); //controller.insertuser(queryvalues); new inserttodb().execute(); ((edittext) findviewbyid(r.id.etcontlabel)).settext(""); ((edittext) findviewbyid(r.id.etqrlabel)).settext(""); getcontlabel.requestfocus(); } else { toast toast = new toast(this); imageview view1 = new imageview(this); view1.setimageresource(r.drawable.fail1); toast.setview(view1); toast.show(); } } public void clicktochange(view view) { trigger = false; this.callhomeactivity(view); } public void callhomeactivity(view view) { intent objintent = new intent(getapplicationcontext(), mainactivity.class); startactivity(objintent); } class inserttodb extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(scantruck.this); pdialog.setmessage("updating database..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } protected string doinbackground(string... args) { string pickticket = getpickticket.gettext().tostring(); string userid = getuserid.gettext().tostring(); string contlabel = getcontlabel.gettext().tostring(); string qrlabel = getqrlabel.gettext().tostring(); // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("pickticket", pickticket)); params.add(new basicnamevaluepair("userid", userid)); params.add(new basicnamevaluepair("contlabel", contlabel)); params.add(new basicnamevaluepair("qrlabel", qrlabel)); // getting json object // note create product url accepts post method jsonobject json = jsonparser.makehttprequest(url_create_product, // check log cat response //log.d("create response", json.tostring()); // check success tag try { int success = json.getint(tag_success); if (success == 1) { // created product toast.maketext(getapplicationcontext(),"dbupdated!", toast.length_short).show(); // closing screen finish(); } else { // failed create product } } catch (jsonexception e) { e.printstacktrace(); }catch (nullpointerexception ex){ ex.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once done pdialog.dismiss(); } }
my php normal post anyway:
<?php /* * following code create new verify row * fields read http post request */ // array json response $response = array(); print_r($_post); // check required fields if (isset($_post["pickticket"]) && isset($_post["userid"])&&isset ($_post["contlabel"])&& isset ($_post["qrlabel"])) { $pickticket = $_post['pickticket']; $userid = $_post['userid']; $contlabel = $_post['contlabel']; $qrlabel = $_post['qrlabel']; // include db connect class require_once __dir__ . '/db_connect.php'; // connecting db $db = new db_connect(); // mysql inserting new row $result = mysql_query("insert verifyqr(pickticket,userid, contlabel, qrlabel) values('$pickticket', '$userid', '$contlabel', '$qrlabel')"); // check if row inserted or not if ($result) { // inserted database $response["success"] = 1; $response["message"] = "product created."; // echoing json response echo json_encode($response); } else { // failed insert row $response["success"] = 0; $response["message"] = "oops! error occurred."; // echoing json response echo json_encode($response); } } else { // required field missing $response["success"] = 0; $response["message"] = "required field(s) missing"; // echoing json response echo json_encode($response); } ?>
i not have enough rep post image contlabel field missing of data though data in edittext boxes. appreciated. , have searched problem extensively nothing quite mine. thank you!
anarchy
in activity have:
new inserttodb().execute(); ((edittext) findviewbyid(r.id.etcontlabel)).settext(""); ((edittext) findviewbyid(r.id.etqrlabel)).settext("");
so calls asynctask , sets edittexts empty string.
since asynctasks run in thread ui thread doesn't wait task complete before resetting contents , guess edittexts cleared before asynctask accesses information want.
you should either:
- move clearing onpostexecute() in asynctask if don't mind delays caused tasks's workload
- or pass values read through constructor asynctask instead of reading them asynctask directly values may have been changed.
if need clarification on of ask , can supply example code.
on side note, should have edittexts , other views want access class level variables. assign them in oncreate , refer them variable name afterwards instead of pulling them layout , casting them anew every time need use them.
Comments
Post a Comment