android.os.NetworkOnMainThreadException -
i have used asynctask in code. showing error have given in logcat.i have created google api emulator of api 18.in not working.but working in api8. whether work in real device of version?
btnlogin.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { new logintask_1().execute(); } });
asynctask
class logintask_1 extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(registeractivity.this); pdialog.setmessage("loging in..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } /** * deleting product * */ protected string doinbackground(string... args) { runonuithread(new runnable() { public void run() { string code = inputcode.gettext().tostring(); userfunctions userfunction = new userfunctions(); log.d("button", "login"); jsonobject json = userfunction.loginuser1(id,code); // check login response try { if (json.getstring(key_success) != null) { loginerrormsg1.settext(""); string res = json.getstring(key_success); if(integer.parseint(res) == 1){ databasehandler1 db = new databasehandler1(getapplicationcontext()); db.adduser(adduser1, adduser2,id, adduser3,adduser4); // launch dashboard screen intent dashboard = new intent(getapplicationcontext(), mainactivity.class); dashboard.putextra("name", id); dashboard.putextra("email", adduser4); //dashboard.putextra(extra_message, id); // close views before launching dashboard dashboard.addflags(intent.flag_activity_clear_top); startactivity(dashboard); // close login screen finish(); } /*else if(integer.parseint(res) == 2){ loginerrormsg.settext("already logged in"); }*/ else{ // error in login loginerrormsg1.settext("invalid code"); } } } catch (jsonexception e) { e.printstacktrace(); } } }); return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog once product deleted pdialog.dismiss(); } }
logcat
03-21 06:47:54.403: e/androidruntime(3265): fatal exception: main 03-21 06:47:54.403: e/androidruntime(3265): android.os.networkonmainthreadexception 03-21 06:47:54.403: e/androidruntime(3265): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1133) 03-21 06:47:54.403: e/androidruntime(3265): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 03-21 06:47:54.403: e/androidruntime(3265): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 03-21 06:47:54.403: e/androidruntime(3265): @ java.net.inetaddress.getallbyname(inetaddress.java:214) 03-21 06:47:54.403: e/androidruntime(3265): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) 03-21 06:47:54.403: e/androidruntime(3265): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 03-21 06:47:54.403: e/androidruntime(3265): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 03-21 06:47:54.403: e/androidruntime(3265): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 03-21 06:47:54.403: e/androidruntime(3265): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 03-21 06:47:54.403: e/androidruntime(3265): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 03-21 06:47:54.403: e/androidruntime(3265): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 03-21 06:47:54.403: e/androidruntime(3265): @ com.androidhive.pushnotifications.jsonparser.getjsonfromurl(jsonparser.java:47) 03-21 06:47:54.403: e/androidruntime(3265): @ com.androidhive.pushnotifications.userfunctions.loginuser(userfunctions.java:46) 03-21 06:47:54.403: e/androidruntime(3265): @ com.androidhive.pushnotifications.loginactivity$logintask$1.run(loginactivity.java:125) 03-21 06:47:54.403: e/androidruntime(3265): @ android.os.handler.handlecallback(handler.java:730) 03-21 06:47:54.403: e/androidruntime(3265): @ android.os.handler.dispatchmessage(handler.java:92) 03-21 06:47:54.403: e/androidruntime(3265): @ android.os.looper.loop(looper.java:137) 03-21 06:47:54.403: e/androidruntime(3265): @ android.app.activitythread.main(activitythread.java:5103) 03-21 06:47:54.403: e/androidruntime(3265): @ java.lang.reflect.method.invokenative(native method) 03-21 06:47:54.403: e/androidruntime(3265): @ java.lang.reflect.method.invoke(method.java:525) 03-21 06:47:54.403: e/androidruntime(3265): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:737) 03-21 06:47:54.403: e/androidruntime(3265): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 03-21 06:47:54.403: e/androidruntime(3265): @ dalvik.system.nativestart.main(native method)
remove runonuithread
runonuithread(new runnable() { public void run() {
your doinbackground
invoked on background thread. don't need runonuithread
network operation.
and return result in doinbackground
, startactivity in onpostexecute
.
edit:
as suggest prosper k upadate ui in onpostexecute
not in doinbackground
loginerrormsg1.settext("invalid code");
also text edittext in activity , pass string doinbackground
btnlogin.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { string code = inputcode.gettext().tostring(); new logintask_1().execute(code); } });
then
protected string doinbackground(string... args) { string value = args[0];
Comments
Post a Comment