java - NetworkOnMainThreadException- Have tried making a new Thread and also ASynchTask -


i've been trying work while. i'm trying send message phone simple server on laptop. keep getting networkonmainthreadexception, i've tried making new thread(new runnable() etc. , asynchtask still getting error , app force closing. have read through 3 or 4 of questions similar none have worked me. here code:

final button post2 = (button) findviewbyid(r.id.postbutton2);      post2.setonclicklistener(new onclicklistener(){          @override         public void onclick(view arg0) {             // todo auto-generated method stub             new thread(new runnable() {                 // todo auto-generated method stub                  @override                 public void run() {                     // todo auto-generated method stub                      message = text.gettext().tostring(); //message string, text edittext.                     text.settext("");                      try {                         clientsocket = new socket("10.0.0.2", 4445);                         printwriter = new printwriter(clientsocket                                 .getoutputstream(), true);                         printwriter.write(message);                          printwriter.flush();                         printwriter.close();                         clientsocket.close();                      } catch (unknownhostexception e) {                         e.printstacktrace();                         toast.maketext(context, e.tostring(),                                 toast.length_short).show();                     } catch (ioexception e) {                         e.printstacktrace();                         toast.maketext(context, e.tostring(),                                 toast.length_short).show();                     }                 }              }).start();         }      }); 

try removing following part code.

message = text.gettext().tostring(); //message string, text edittext.                     text.settext(""); 

it not right in thread.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -