java - This application may be doing too much work in its main thread -
i using following piece of code add item mysql database.when button clicked,it should begin async task uploads data server.but when run error message "this application may doing work in main thread" logcat.how can fix code problem,thanks.
public class newproductactivity extends activity { // progress dialog private progressdialog pdialog; jsonparser jsonparser = new jsonparser(); edittext inputname; edittext inputprice; edittext inputdesc; // url create new product private static string url_create_product = "http://localhost/android_connect/create_product.php"; // json node names private static final string tag_success = "success"; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.add_product); // edit text inputname = (edittext) findviewbyid(r.id.inputname); inputprice = (edittext) findviewbyid(r.id.inputprice); inputdesc = (edittext) findviewbyid(r.id.inputdesc); // create button button btncreateproduct = (button) findviewbyid(r.id.btncreateproduct); // button click event btncreateproduct.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { // creating new product in background thread new createnewproduct().execute(); } }); } /** * background async task create new product * */ class createnewproduct extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(newproductactivity.this); pdialog.setmessage("creating product.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } /** * creating product * */ protected string doinbackground(string... args) { string name = inputname.gettext().tostring(); string price = inputprice.gettext().tostring(); string description = inputdesc.gettext().tostring(); // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("name", name)); params.add(new basicnamevaluepair("price", price)); params.add(new basicnamevaluepair("description", description)); // getting json object // note create product url accepts post method jsonobject json = jsonparser.makehttprequest(url_create_product, "post", params); // check log cat fro response log.d("create response", json.tostring()); // check success tag try { int success = json.getint(tag_success); if (success == 1) { // created product intent = new intent(getapplicationcontext(), allproductsactivity.class); startactivity(i); // closing screen finish(); } else { // failed create product } } catch (jsonexception e) { e.printstacktrace(); } return null; }
this logcat output:
05-08 12:51:26.761: i/choreographer(762): skipped 31 frames! application may doing work on main thread. 05-08 12:51:28.632: w/system.err(762): org.apache.http.conn.httphostconnectexception: "connection localhost refused 05-08 12:51:28.672: w/system.err(762): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:183) 05-08 12:51:28.672: w/system.err(762): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 05-08 12:51:28.712: w/system.err(762): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 05-08 12:51:28.712: w/system.err(762): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 05-08 12:51:28.832: w/system.err(762): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 05-08 12:51:28.902: w/system.err(762): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 05-08 12:51:29.012: w/system.err(762): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 05-08 12:51:29.072: w/system.err(762): @ com.example.androidhive.jsonparser.makehttprequest(jsonparser.java:51) 05-08 12:51:29.072: w/system.err(762): @ com.example.androidhive.newproductactivity$createnewproduct.doinbackground(newproductactivity.java:97) 05-08 12:51:29.172: w/system.err(762): @ com.example.androidhive.newproductactivity$createnewproduct.doinbackground(newproductactivity.java:1) 05-08 12:51:29.252: w/system.err(762): @ android.os.asynctask$2.call(asynctask.java:287) 05-08 12:51:29.312: w/system.err(762): @ java.util.concurrent.futuretask.run(futuretask.java:234) 05-08 12:51:29.322: w/system.err(762): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 05-08 12:51:29.342: w/system.err(762): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 05-08 12:51:29.342: w/system.err(762): @ java.lang.thread.run(thread.java:856) 05-08 12:51:29.372: w/system.err(762): caused by: java.net.connectexception: failed connect /127.0.0.1 (port 80): connect failed: econnrefused (connection refused) 05-08 12:51:29.442: w/system.err(762): @ libcore.io.iobridge.connect(iobridge.java:114) 05-08 12:51:29.493: w/system.err(762): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 05-08 12:51:29.552: w/system.err(762): @ java.net.plainsocketimpl.connect(plainsocketimpl.java:459) 05-08 12:51:29.552: w/system.err(762): @ java.net.socket.connect(socket.java:842) 05-08 12:51:29.572: w/system.err(762): @ org.apache.http.conn.scheme.plainsocketfactory.connectsocket(plainsocketfactory.java:119) 05-08 12:51:29.652: w/system.err(762): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:144) 05-08 12:51:29.692: w/system.err(762): ... 14 more 05-08 12:51:29.763: w/system.err(762): caused by: libcore.io.errnoexception: connect failed: econnrefused (connection refused) 05-08 12:51:29.872: w/system.err(762): @ libcore.io.posix.connect(native method) 05-08 12:51:29.872: w/system.err(762): @ libcore.io.blockguardos.connect(blockguardos.java:85) 05-08 12:51:29.892: w/system.err(762): @ libcore.io.iobridge.connecterrno(iobridge.java:127) 05-08 12:51:29.992: w/system.err(762): @ libcore.io.iobridge.connect(iobridge.java:112) 05-08 12:51:29.992: w/system.err(762): ... 19 more 05-08 12:51:30.102: e/buffer error(762): error converting result java.lang.nullpointerexception: lock == null 05-08 12:51:30.212: e/json parser(762): error parsing data org.json.jsonexception: end of input @ character 0 of 05-08 12:51:30.302: w/dalvikvm(762): threadid=14: thread exiting uncaught exception (group=0x40a71930) 05-08 12:51:30.602: e/androidruntime(762): fatal exception: asynctask #4 05-08 12:51:30.602: e/androidruntime(762): java.lang.runtimeexception: error occured while executing doinbackground() 05-08 12:51:30.602: e/androidruntime(762): @ android.os.asynctask$3.done(asynctask.java:299) 05-08 12:51:30.602: e/androidruntime(762): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 05-08 12:51:30.602: e/androidruntime(762): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 05-08 12:51:30.602: e/androidruntime(762): @ java.util.concurrent.futuretask.run(futuretask.java:239) 05-08 12:51:30.602: e/androidruntime(762): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 05-08 12:51:30.602: e/androidruntime(762): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 05-08 12:51:30.602: e/androidruntime(762): @ java.lang.thread.run(thread.java:856) 05-08 12:51:30.602: e/androidruntime(762): caused by: java.lang.nullpointerexception 05-08 12:51:30.602: e/androidruntime(762): @ com.example.androidhive.newproductactivity$createnewproduct.doinbackground(newproductactivity.java:101) 05-08 12:51:30.602: e/androidruntime(762): @ com.example.androidhive.newproductactivity$createnewproduct.doinbackground(newproductactivity.java:1) 05-08 12:51:30.602: e/androidruntime(762): @ android.os.asynctask$2.call(asynctask.java:287) 05-08 12:51:30.602: e/androidruntime(762): @ java.util.concurrent.futuretask.run(futuretask.java:234) 05-08 12:51:30.602: e/androidruntime(762): ... 3 more 05-08 12:51:32.542: i/choreographer(762): skipped 395 frames! application may doing work on main thread. 05-08 12:51:32.752: i/choreographer(762): skipped 54 frames! application may doing work on main thread. 05-08 12:51:33.892: i/choreographer(762): skipped 295 frames! application may doing work on main thread. 05-08 12:51:34.132: i/choreographer(762): skipped 58 frames! application may doing work on main thread. 05-08 12:51:35.762: e/windowmanager(762): activity com.example.androidhive.newproductactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview{40d3d5e0 v.e..... r.....id 0,0-329,175} added here 05-08 12:51:35.762: e/windowmanager(762): android.view.windowleaked: activity com.example.androidhive.newproductactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview{40d3d5e0 v.e..... r.....id 0,0-329,175} added here 05-08 12:51:35.762: e/windowmanager(762): @ android.view.viewrootimpl.<init>(viewrootimpl.java:354) 05-08 12:51:35.762: e/windowmanager(762): @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:216) 05-08 12:51:35.762: e/windowmanager(762): @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) 05-08 12:51:35.762: e/windowmanager(762): @ android.app.dialog.show(dialog.java:281) 05-08 12:51:35.762: e/windowmanager(762): @ com.example.androidhive.newproductactivity$createnewproduct.onpreexecute(newproductactivity.java:78) 05-08 12:51:35.762: e/windowmanager(762): @ android.os.asynctask.executeonexecutor(asynctask.java:586) 05-08 12:51:35.762: e/windowmanager(762): @ android.os.asynctask.execute(asynctask.java:534) 05-08 12:51:35.762: e/windowmanager(762): @ com.example.androidhive.newproductactivity$1.onclick(newproductactivity.java:57) 05-08 12:51:35.762: e/windowmanager(762): @ android.view.view.performclick(view.java:4204) 05-08 12:51:35.762: e/windowmanager(762): @ android.view.view$performclick.run(view.java:17355) 05-08 12:51:35.762: e/windowmanager(762): @ android.os.handler.handlecallback(handler.java:725) 05-08 12:51:35.762: e/windowmanager(762): @ android.os.handler.dispatchmessage(handler.java:92) 05-08 12:51:35.762: e/windowmanager(762): @ android.os.looper.loop(looper.java:137) 05-08 12:51:35.762: e/windowmanager(762): @ android.app.activitythread.main(activitythread.java:5041) 05-08 12:51:35.762: e/windowmanager(762): @ java.lang.reflect.method.invokenative(native method) 05-08 12:51:35.762: e/windowmanager(762): @ java.lang.reflect.method.invoke(method.java:511) 05-08 12:51:35.762: e/windowmanager(762): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-08 12:51:35.762: e/windowmanager(762): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-08 12:51:35.762: e/windowmanager(762): @ dalvik.system.nativestart.main(native method) 05-08 12:51:35.812: i/choreographer(762): skipped 172 frames! application may doing work on main thread. 05-08 12:51:36.172: i/process(762): sending signal. pid: 762 sig: 9
whilst doesn't appear related, in logcat post, code executing:
jsonobject json = jsonparser.makehttprequest(url_create_product, "post", params);
when code terminated. appears request failing ("unable connect localhost"). when request fails else getting onto the ui thread somehow. either way, exception should trapped , handled more cleanly.
Comments
Post a Comment