java - Try Catch Block Not Working Inside OnClickListener() -


this question has answer here:

i working on project control lights using arduino + ethernet shield + android app. using android studio app development purpose. issue is, try-catch block has been implemented inside onclicklistener() doesnt seem work. new android app development , cant think of solution same. app installed buttons not perform function. i.e. server doesnt receive package. actually, targetsdkversion set 8, hence holo theme , buttons worked properly. once set 22 (lollipop) material theme gets applied default , buttons no longer work. thanking in advance.

public void led(string s) throws exception {      byte[] b=(s.getbytes());     if(isonline())     {      serverhostname1 = new string ("192.168.1.177");     ip = inetaddress.getbyname(serverhostname1);      d1 = new datagramsocket();//}      try{         send =  new datagrampacket(b,b.length, ip, 8032);        }catch(exception e){      }       d1.send(send);      d1.setsotimeout(10000);     d1.receive(rec);      modifiedsentence =   new string(rec.getdata());     inetaddress returnipaddress = rec.getaddress();      toast.maketext(getapplicationcontext(),"reply server:"+returnipaddress,toast.length_long).show();      d1.close();      }     else     {         toast.maketext(getapplicationcontext(),"no network",toast.length_long).show();     } }  public void oncreate(bundle savedinstancestate) {      super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     button on= (button)findviewbyid(r.id.on);     button off= (button)findviewbyid(r.id.off);       on.setonclicklistener(new view.onclicklistener() {          public void onclick(view v) {             // todo auto-generated method stub              try {                 //arduinoactivity a=new arduinoactivity();                  led("1");                   toast.maketext(getapplicationcontext(),"on",toast.length_short).show();               } catch (exception e) {                 // todo auto-generated catch block                 system.out.println("error::"+e);             }          }     });       off.setonclicklistener(new view.onclicklistener() {          public void onclick(view v) {             // todo auto-generated method stub              try {                 //arduinoactivity b=new arduinoactivity();                 led("2");                 toast.maketext(getapplicationcontext(), "off",toast.length_short).show();              } catch (exception e) {                  //todo auto-generated catch block                 system.out.println("error::"+e);             }          }     });   } } 

the issue not try-catch block networking operations being carried out inside ui thread (sending of packet) not allowed in android versions 3.0+ . solution make use of asynctask same. thank :)


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

python - How to remove the Xframe Options header in django? -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -