Android Socket Error or crash -


i'm using following code send data tcp server got errors. idea? need use thread? can run directly on function ?

the msg & ipo & proto editboxes in layout. think problem using socket!

    socket clientsocketo; string msgout="nothing happend"; textview ad;  public void senddata(view v) throws ioexception {     edittext aa = (edittext)findviewbyid(r.id.edittext01);     string msg = aa.gettext().tostring();      edittext ab = (edittext)findviewbyid(r.id.edittext);     string ipo = ab.gettext().tostring();      edittext ac = (edittext)findviewbyid(r.id.edittext2);      int porto  = integer.parseint(ac.gettext().tostring());       inetaddress serveraddress = inetaddress.getbyname(ipo);     try{         clientsocketo = new socket(serveraddress,porto);         objectoutputstream oos = new objectoutputstream(clientsocketo.getoutputstream());         oos.writeobject("hello world");          objectinputstream ois = new objectinputstream(clientsocketo.getinputstream());         string massagedf = (string)ois.readobject();         ad.settext(massagedf);         // mhandler.sendmessage(servermessage);          oos.close();         ois.close();     }     catch(exception e){         ad.settext("error");     }  }  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     ad = (textview) findviewbyid(r.id.textview); } 

network operations must handled in asynctask. allows network operations continue without blocking main thread. best way use asynctask create sub-class. this:

public class senddatatask extends asynctask<void, void, void> {      socket clientsocketto;      string msg;     string ipo;     int porto;     inetaddress serveraddress;      public senddatatask(view v){         edittext aa = (edittext) v.findviewbyid(r.id.edittext01);         msg = aa.gettext().tostring          edittext ab = (edittext) v.findviewbyid(r.id.edittext);         ipo = ab.gettext().tostring();          edittext ac = (edittext) v.findviewbyid(r.id.edittext2);         porto = integer.parseint(ac.gettext().tostring());         serveraddress = inetaddress.getbyname(ipo);      }      protected void doinbackground(void... arg){         try{             clientsocketto = new socket(serveraddress,porto);             objectoutputstream oos = new objectoutputstream(clientsocketto.getoutputstream());             oos.weriteobject("hello world")              objectinputstream ois = new objectinputstream(clientsocketto.getinputstream());             string massagedf = (string) ois.readobject();             ad.settext(massagedf);              oos.close();             ois.close();          }catch (exception e){          }      }      protected void onpostexecute(void result){      }  } 

to call instantiate class passing view , call execute method:

new senddatatask(yourview).execute(); 

none of code has been tested... hope helps!

more information: http://developer.android.com/reference/android/os/asynctask.html http://androidsrc.net/android-client-server-using-sockets-client-implementation/


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -