java - Why AsyncTask to avoid "NetworkOnMainThreadException" is not working? -


trying implement simple udp client/server datagram between androidstudio-java application client, visual studio c# server. sure of server side being working.

here udp client , on buttonclick udp message should sent localhost on port 15000 "for now".

my stacktrace popped android.os.networkonmainthreadexception error. found here can use easy solution import strictmode , set new policy permitall(). still application couldn't work , literally nothing happens on buttonclick "no exception trace + no received message" , here code:

        buttonone.setonclicklistener(                     new button.onclicklistener() {                         public void onclick(view v) {                             textview textone = (textview) findviewbyid(r.id.testtext);                             textone.settext("hi");                              string host = "127.0.0.1"; // localhost                             int port = 15000;                             string message = "test";                             datagramsocket dsocket = null;                              if (android.os.build.version.sdk_int > 9)                             {                                 strictmode.threadpolicy policy =                                          new strictmode.threadpolicy.builder().permitall().build();                                 strictmode.setthreadpolicy(policy);                             }                              try {                                 // internet address of specified host                                 inetaddress address = inetaddress.getbyname(host);                                  // wrap packet                                 datagrampacket packet = new datagrampacket(                                         message.getbytes(),                                         message.length(),                                         address, port);                                  // create datagram socket, send packet through it, close it.                                 dsocket = new datagramsocket();                                 dsocket.send(packet);                                 dsocket.close();                             } catch (exception e) {                                 e.printstacktrace();                             }                         }                     }             ); 

then found here here it's not recommended use strictmode , need use asynctask. on android documentation says "asynctask must subclassed used. subclass override @ least 1 method (doinbackground(params...)), , override second 1 (onpostexecute(result).)" don't because each time add async within mainactivity class errors , it's frustrating..

is't okay use strictmode simple task? if yes, why it's not working? if no, can tell me please how import asynctask piece of code? , should use params, progress, result functions??

since packet send , forget , not monitor progress or @ end, not need async task. need start network activity in new thread. code below, may have minor compilation issues since not have access 1 right now.

buttonone.setonclicklistener(                     new button.onclicklistener() {                         public void onclick(view v) {                             textview textone = (textview) findviewbyid(r.id.testtext);                             textone.settext("hi");                               string message = "test";                             thread networkthread = new thread() {                                 string host = "127.0.0.1"; // localhost                               int port = 15000;                               datagramsocket dsocket = null;                                public void run() {                                 try {                                 // internet address of specified host                                 inetaddress address = inetaddress.getbyname(host);                                  // wrap packet                                 datagrampacket packet = new datagrampacket(                                         message.getbytes(),                                         message.length(),                                         address, port);                                  // create datagram socket, send packet through it, close it.                                 dsocket = new datagramsocket();                                 dsocket.send(packet);                                 dsocket.close();                               } catch (exception e) {                                 e.printstacktrace();                             }//catch                           }//run                          };// networkthread                          networkthread.start();//networkthread.start()                         }//onclick                     }//onclicklistener             );//setonclicklistener 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -