java - How to get results onPostExcute in AsyncTask? -


i've been using asynctask few days. old code works on api 8 runonuithred dosn't work on >api 18. want send data db json in doinbackground() , data in onpostexecute(). how separate code 2 parts?

class getproductdetails extends asynctask<string, string, integer> {      @override     protected void onpreexecute() {        //bla-bla-bla     }      protected integer doinbackground(string... args) {                int success = 0;                 try {                     list<namevaluepair> params = new arraylist<namevaluepair>();                     params.add(new basicnamevaluepair("pid", pid));                      jsonobject json = jsonparser.makehttprequest(                             url_product_detials, "get", params);                     success = json.getint(tag_success);                  } catch (exception e) {                     e.printstacktrace();                 }          return success;     }      protected void onpostexecute(integer success) {           pdialog.dismiss();           jsonarray productobj = json.getjsonarray(tag_product);            jsonobject product = productobj.getjsonobject(0);            if (success == 1) {                  txtname = (textview) findviewbyid(r.id.tvname);                 txtprice = (textview) findviewbyid(r.id.tvprice);                 txtdesc = (textview) findviewbyid(r.id.tvdescription);                 txtcurrent = (textview) findviewbyid(r.id.tvcurrent);                 txtturn = (textview) findviewbyid(r.id.tvturn);                  txtname.settext(product.getstring(tag_name));                 txtprice.settext(product.getstring(tag_price));                 txtdesc.settext(product.getstring(tag_description));                 txtcurrent.settext(product.getstring("current"));                 txtturn.settext(product.getstring("turn"));            }else{ }        } } 

enter image description hereyou let doinbackground return jsonobject json , use in onpostexecute.

then check if ok, check inside onpostexecute.

something like:

class getproductdetails extends asynctask<string, string, jsonobject> {      @override     protected void onpreexecute() {         //bla-bla-bla     }      protected jsonobject doinbackground(string... args) {         jsonobject json = null;          try {             list<namevaluepair> params = new arraylist<namevaluepair>();             params.add(new basicnamevaluepair("pid", pid));              json = jsonparser.makehttprequest(                     url_product_detials, "get", params);          } catch (exception e) {             e.printstacktrace();         }          return json;     }      protected void onpostexecute(jsonobject json) {          pdialog.dismiss();         if (json == null) return; // check if json null too! if null went wrong (handle it, used return example)          int success = json.getint(tag_success);;          if (success == 1) {             jsonarray productobj = json.getjsonarray(tag_product);         jsonobject product = productobj.getjsonobject(0);             txtname = (textview) findviewbyid(r.id.tvname);             txtprice = (textview) findviewbyid(r.id.tvprice);             txtdesc = (textview) findviewbyid(r.id.tvdescription);             txtcurrent = (textview) findviewbyid(r.id.tvcurrent);             txtturn = (textview) findviewbyid(r.id.tvturn);              txtname.settext(product.getstring(tag_name));             txtprice.settext(product.getstring(tag_price));             txtdesc.settext(product.getstring(tag_description));             txtcurrent.settext(product.getstring("current"));             txtturn.settext(product.getstring("turn"));          }else{          }     } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -