Android : android.os.networkonmainthreadexception -


this question has answer here:

i'm having problem android login functionality, getting , using android device 4.1.... when run application terminate , console show error :

below code : please advice

public class signup extends activity implements onclicklistener { // ip address send data string serverip = "192.168.1.51";    edittext ed_employee_username,ed_employee_password; edittext ed_new_username,ed_new_password,ed_confirm_password; button btn_signup,btn_cancle_signup; string e_username,e_password,n_username,n_password,c_password,blank = ""; string start,code,end,data; socket client,clientsocket; printwriter printwriter; db_adapter database; serversocket serversocket; inputstreamreader inputstreamreader; bufferedreader bufferedreader; string employee_username_padd,employee_password_padd,imei,msg,result,username; intent i;     @override       // used oncreate method     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_signup);         ed_employee_username = (edittext)findviewbyid(r.id.ed_employee_username);         ed_employee_password = (edittext)findviewbyid(r.id.ed_employee_password);         ed_new_username = (edittext)findviewbyid(r.id.ed_new_username);         ed_new_password = (edittext)findviewbyid(r.id.ed_new_password);         ed_confirm_password = (edittext)findviewbyid(r.id.ed_confirm_password);         database = new db_adapter(getapplicationcontext(), "wbs", null, 1);         btn_signup = (button)findviewbyid(r.id.btn_signup);         btn_cancle_signup = (button)findviewbyid(r.id.btn_cancle_signup);          btn_signup.setonclicklistener(this);         btn_cancle_signup.setonclicklistener(this);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.signup, menu);         return true;     }      @override     public void onclick(view v) {         // todo auto-generated method stub         switch (v.getid())         {             case r.id.btn_signup:                 e_username = ed_employee_username.gettext().tostring();                 e_password = ed_employee_password.gettext().tostring();                 n_username = ed_new_username.gettext().tostring();                 n_password = ed_new_password.gettext().tostring();                 c_password = ed_confirm_password.gettext().tostring();                  if(e_username.equals(blank))                 {                     toast.maketext(getapplicationcontext(), "please enter employee username", toast.length_short).show();                 }                 else if(e_password.equals(blank) || e_password.length() <6)                 {                     ed_employee_password.seterror("you must have 6 character’s in password");                     return;                 }                 else if(n_username.equals(blank))                 {                     toast.maketext(getapplicationcontext(), "please enter new username", toast.length_short).show();                 }                 else if(n_password.equals(blank) || n_password.length() <6)                 {                     ed_new_password.seterror("you must have 6 character’s in password");                     return;                 }                 else if(c_password.equals(blank) || c_password.length() <6)                 {                     ed_confirm_password.seterror("you must have 6 character’s in password");                     return;                 }                 else if(n_password.equals(c_password))                 {                     telephonymanager tm = (telephonymanager)getsystemservice(context.telephony_service);                     imei = tm.getdeviceid();//imei no captured                     send();                     receive();                     if(username.equals(e_username))                     {                         database.signup_detail(n_username, n_password, imei);                         toast.maketext(getapplicationcontext(), " new user registered successfully", toast.length_short).show();                         = new intent(getapplicationcontext(),login.class);                         startactivity(i);                         finish();                     }                     else                     {                         toast.maketext(getapplicationcontext(), "user registered mobile device", toast.length_short).show();                                         }                 }                 else                 {                     toast.maketext(getapplicationcontext(), "new password , confirm password not match", toast.length_short).show();                 }              break;             case r.id.btn_cancle_signup:                 = new intent(getapplicationcontext(),login.class);                 startactivity(i);                 finish();             break;         }     }      public void send()     {         toast.maketext(getapplicationcontext(), "in send...", toast.length_short).show();         start = "@str";         code =  "01";         end = "000$";         //padding logic          employee_username_padd = e_username;         employee_password_padd = e_password;         int fixlen= 10;         int eidlen= e_username.length();         int epasslen= e_password.length();          if(fixlen>eidlen)         {             for(int i=0;i<(fixlen-eidlen);i++)             {                 employee_username_padd="*"+employee_username_padd;             }         }         if(fixlen>epasslen)         {             for(int i=0;i<(fixlen-epasslen);i++)             {                 employee_password_padd="*"+employee_password_padd;             }         }          data = start+code+employee_username_padd+employee_password_padd+imei+end;         try {             client=new socket(serverip,2222);             printwriter=new printwriter(client.getoutputstream(),true);             printwriter.write(data);             printwriter.flush();             printwriter.close();             //receive();             client.close();          } catch (unknownhostexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     }      public string receive()     {         try {             //toast.maketext(getapplicationcontext(), "in receive", toast.length_short).show();             serversocket = new serversocket(1111);             clientsocket = serversocket.accept();             inputstreamreader = new inputstreamreader(clientsocket.getinputstream());             bufferedreader = new bufferedreader(inputstreamreader); //get client message             msg = bufferedreader.readline();             toast.maketext(getapplicationcontext(), msg, toast.length_long).show();             string start1= msg.substring(0,4);             string code1= msg.substring(4, 6);             string result1= msg.substring(6, 16);             string end1= msg.substring(16, 20);              if(start1.equals("@str") && code1.equals("01") && end1.equals("999$"))             {                 /*toast.maketext(getapplicationcontext(), start1, toast.length_short).show();                 toast.maketext(getapplicationcontext(), code1, toast.length_short).show();                 toast.maketext(getapplicationcontext(), result1, toast.length_short).show();                 toast.maketext(getapplicationcontext(), end1, toast.length_short).show();*/                  result = result1;                 remove_padding();             }else             {                 /*toast.maketext(getapplicationcontext(), "in else"+start1, toast.length_short).show();                 toast.maketext(getapplicationcontext(), "in else"+code1, toast.length_short).show();                 toast.maketext(getapplicationcontext(), "in else"+result1, toast.length_short).show();                 toast.maketext(getapplicationcontext(), "in else"+end1, toast.length_short).show();*/                 result = result1;                 remove_padding();             }          } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         return result;     }     public string remove_padding()     {         //string starusername=usernamepasswordimei.substring(0,10);          char star='*';                  int count=0;                      if(result.charat(0) == star)                     {                         for(int i=0;i<result.length();i++)                         {                              if(result.charat(i) == star)                             {                                 count++;                             }                             /*break;*/                         }//end                         username=result.substring(count,result.length());                     }                     else                     {                         system.out.println("user name string doesn't start *");                         username=result;                     }                       return username;      } 

after run shows below error :

07-18 15:13:09.502: e/androidruntime(32249): fatal exception: main 07-18 15:13:09.502: e/androidruntime(32249): android.os.networkonmainthreadexception 07-18 15:13:09.502: e/androidruntime(32249):    @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 07-18 15:13:09.502: e/androidruntime(32249):    @ libcore.io.blockguardos.connect(blockguardos.java:84) 07-18 15:13:09.502: e/androidruntime(32249):    @ libcore.io.iobridge.connecterrno(iobridge.java:127) 07-18 15:13:09.502: e/androidruntime(32249):    @ libcore.io.iobridge.connect(iobridge.java:112) 07-18 15:13:09.502: e/androidruntime(32249):    @ java.net.plainsocketimpl.connect(plainsocketimpl.java:192) 07-18 15:13:09.502: e/androidruntime(32249):    @ java.net.plainsocketimpl.connect(plainsocketimpl.java:172) 07-18 15:13:09.502: e/androidruntime(32249):    @ java.net.socket.startupsocket(socket.java:566) 07-18 15:13:09.502: e/androidruntime(32249):    @ java.net.socket.tryalladdresses(socket.java:127) 07-18 15:13:09.502: e/androidruntime(32249):    @ java.net.socket.<init>(socket.java:177) 07-18 15:13:09.502: e/androidruntime(32249):    @ java.net.socket.<init>(socket.java:149) 07-18 15:13:09.502: e/androidruntime(32249):    @ com.ami.wbs.signup.send(signup.java:161) 07-18 15:13:09.502: e/androidruntime(32249):    @ com.ami.wbs.signup.onclick(signup.java:102) 07-18 15:13:09.502: e/androidruntime(32249):    @ android.view.view.performclick(view.java:4117) 07-18 15:13:09.502: e/androidruntime(32249):    @ android.view.view$performclick.run(view.java:17041) 07-18 15:13:09.502: e/androidruntime(32249):    @ android.os.handler.handlecallback(handler.java:615) 07-18 15:13:09.502: e/androidruntime(32249):    @ android.os.handler.dispatchmessage(handler.java:92) 07-18 15:13:09.502: e/androidruntime(32249):    @ android.os.looper.loop(looper.java:137) 07-18 15:13:09.502: e/androidruntime(32249):    @ android.app.activitythread.main(activitythread.java:4747) 07-18 15:13:09.502: e/androidruntime(32249):    @ java.lang.reflect.method.invokenative(native method) 07-18 15:13:09.502: e/androidruntime(32249):    @ java.lang.reflect.method.invoke(method.java:511) 07-18 15:13:09.502: e/androidruntime(32249):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 07-18 15:13:09.502: e/androidruntime(32249)`enter code here`:   @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 07-18 15:13:09.502: e/androidruntime(32249):    @ dalvik.system.nativestart.main(native method) 

you can not use network operations main ui thread. have use in different threads ( better use asynctask)

for more info check doc here. doc

the exception thrown when application attempts perform networking operation on main thread.

this thrown applications targeting honeycomb sdk or higher. applications targeting earlier sdk versions allowed networking on main event loop threads, it's heavily discouraged. see document designing responsiveness.

also see strictmode.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -