i try parse data using json , asynctask. getting error on these line:
jsonobject json = jsonparser.makehttprequest(url_product_detials, "get", params); this code:
public class editwatchlistproducts extends activity { edittext txtname; edittext txtprice; button btnsave; button btndelete; string pid; private progressdialog pdialog; jsonparser jsonparser = new jsonparser(); private static final string url_product_detials = "http://192.168.2.22/android_connect/get_product_details.php"; private static final string url_update_product = "http://192.168.2.22/android_connect/update_product.php"; private static final string url_delete_product = "http://192.168.2.22/android_connect/delete_product.php"; private static final string tag_success = "success"; private static final string tag_product = "product"; private static final string tag_id = "product_id"; private static final string tag_name = "product_name"; private static final string tag_price = "target_price"; private static final string tag_description = "retailer"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.edit_watchlist_product); btnsave = (button) findviewbyid(r.id.btnsave); btndelete = (button) findviewbyid(r.id.btndelete); intent = getintent(); pid = i.getstringextra(tag_id); new getproductdetails().execute(); btnsave.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { new saveproductdetails().execute(); } }); btndelete.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { new deleteproduct().execute(); } }); } class getproductdetails extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(editwatchlistproducts.this); pdialog.setmessage("loading product details. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } protected string doinbackground(string... params) { runonuithread(new runnable() { public void run() { // check success tag int success; try { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("product_id", pid)); // getting product details making http request // note product details url use request jsonobject json = jsonparser.makehttprequest( url_product_detials, "get", params); success = json.getint(tag_success); if (success == 1) { jsonarray productobj = json .getjsonarray(tag_product); // json array jsonobject product = productobj.getjsonobject(0); txtname = (edittext) findviewbyid(r.id.inputname); txtprice = (edittext) findviewbyid(r.id.inputprice); txtdesc = (edittext) findviewbyid(r.id.inputdesc); txtname.settext(product.getstring(tag_name)); txtprice.settext(product.getstring(tag_price)); txtdesc.settext(product.getstring(tag_description)); }else{ // product pid not found } } catch (jsonexception e) { e.printstacktrace(); } } }); return null; } protected void onpostexecute(string file_url) { pdialog.dismiss(); } } class saveproductdetails extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(editwatchlistproducts.this); pdialog.setmessage("saving product ..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } protected string doinbackground(string... args) { string name = txtname.gettext().tostring(); string price = txtprice.gettext().tostring(); string description = txtdesc.gettext().tostring(); list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair(tag_id, pid)); params.add(new basicnamevaluepair(tag_name, name)); params.add(new basicnamevaluepair(tag_price, price)); params.add(new basicnamevaluepair(tag_description, description)); jsonobject json = jsonparser.makehttprequest(url_update_product, "post", params); try { int success = json.getint(tag_success); if (success == 1) { intent in = new intent(getapplicationcontext(), watchlistproducts.class); startactivity(in); } else { // failed update product } } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url) { pdialog.dismiss(); } } class deleteproduct extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(editwatchlistproducts.this); pdialog.setmessage("deleting product..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } protected string doinbackground(string... args) { int success; try { list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("product_id", pid)); jsonobject json = jsonparser.makehttprequest( url_delete_product, "post", params); log.d("delete product", json.tostring()); success = json.getint(tag_success); if (success == 1) { intent in = new intent(getapplicationcontext(), watchlistproducts.class); startactivity(in); } } catch (jsonexception e) { e.printstacktrace(); } return null; } protected void onpostexecute(string file_url) { pdialog.dismiss(); } } } edit:
the same code working on separate project.when have implemented project time ly getting following exceptions...
what's wrong in code ???
edit: updated log
07-19 16:43:31.018: e/androidruntime(3009): fatal exception: main 07-19 16:43:31.018: e/androidruntime(3009): android.os.networkonmainthreadexception 07-19 16:43:31.018: e/androidruntime(3009): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1084) 07-19 16:43:31.018: e/androidruntime(3009): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:391) 07-19 16:43:31.018: e/androidruntime(3009): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:242) 07-19 16:43:31.018: e/androidruntime(3009): @ java.net.inetaddress.getallbyname(inetaddress.java:220) 07-19 16:43:31.018: e/androidruntime(3009): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) 07-19 16:43:31.018: e/androidruntime(3009): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 07-19 16:43:31.018: e/androidruntime(3009): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 07-19 16:43:31.018: e/androidruntime(3009): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 07-19 16:43:31.018: e/androidruntime(3009): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 07-19 16:43:31.018: e/androidruntime(3009): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 07-19 16:43:31.018: e/androidruntime(3009): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 07-19 16:43:31.018: e/androidruntime(3009): @ com.example.androidbestinuk.jsonparser.makehttprequest(jsonparser.java:60) 07-19 16:43:31.018: e/androidruntime(3009): @ com.example.androidbestinuk.editwatchlistproducts$getproductdetails$1.run(editwatchlistproducts.java:131) 07-19 16:43:31.018: e/androidruntime(3009): @ android.os.handler.handlecallback(handler.java:605) 07-19 16:43:31.018: e/androidruntime(3009): @ android.os.handler.dispatchmessage(handler.java:92) 07-19 16:43:31.018: e/androidruntime(3009): @ android.os.looper.loop(looper.java:137) 07-19 16:43:31.018: e/androidruntime(3009): @ android.app.activitythread.main(activitythread.java:4340) 07-19 16:43:31.018: e/androidruntime(3009): @ java.lang.reflect.method.invokenative(native method) 07-19 16:43:31.018: e/androidruntime(3009): @ java.lang.reflect.method.invoke(method.java:511) 07-19 16:43:31.018: e/androidruntime(3009): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 07-19 16:43:31.018: e/androidruntime(3009): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 07-19 16:43:31.018: e/androidruntime(3009): @ dalvik.system.nativestart.main(native method)
you have runonuithread runs on ui thread , have this
jsonobject json = jsonparser.makehttprequest( url_product_detials, "get", params); from comments above code getting product details making http request. making http request on ui thread. networkonmainthreadexception.
public final void runonuithread (runnable action)added in api level 1
runs specified action on ui thread. if current thread ui thread, action executed immediately. if current thread not ui thread, action posted event queue of ui thread.
parameters
actionaction run on ui thread
so remove runonuithread , update ui in onpreexecute , onpostexecute. make http request in doinbackground.
Comments
Post a Comment