java - SocketTimeoutException crashing AsyncTask in Android 4.x -


i'm having trouble case socketchannel.connect times out. code bellow works fine in old android 2.3 phone , old android 3.2 tablet crashes in android 4.2.2:

       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         tvresult = (textview) findviewbyid(r.id.tvresult);         btntest = (button) this.findviewbyid(r.id.btntest);         btntest.setonclicklistener(dotest);     }      protected view.onclicklistener dotest = new view.onclicklistener() {         @override         public void onclick(view v) {             new asynctest().execute();         }     };      private class asynctest extends asynctask {         private string result = "";          @override         protected void doinbackground(void... params) {             inetsocketaddress addr = new inetsocketaddress("192.168.0.13", 4040);             boolean bconnect = false;             try {                 socketchannel clntchan = socketchannel.open();                 bconnect = clntchan.connect(addr);                 if (bconnect) {                     clntchan.close();                     result = "connected";                 } else {                     result = "did not connected";                 }             } catch (exception e) {                 log.e(tag, "exception " + e.getmessage());                 result = "exception " + e.getmessage();             }             return null;         }          @override         protected void onpostexecute(void ret) {             tvresult.settext (result);         }     }   

i testing case there no 1 listening on port 4040 @ 192.168.0.13. in android 2.3 try/catch catch "connection timeout" exception (as expected). in android 4.x following:

      fatal exception: asynctask #1     java.lang.runtimeexception: error occured while executing doinbackground()         @ android.os.asynctask$3.done(asynctask.java:299)         @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352)         @ java.util.concurrent.futuretask.setexception(futuretask.java:219)         @ java.util.concurrent.futuretask.run(futuretask.java:239)         @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230)         @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080)         @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573)         @ java.lang.thread.run(thread.java:838)     caused by: java.lang.assertionerror: java.net.sockettimeoutexception: failed connect /192.168.0.13 (port 4040) after 90000ms         @ libcore.io.iobridge.connect(iobridge.java:102)         @ java.nio.socketchannelimpl.connect(socketchannelimpl.java:177)         @ br.com.dqsoft.testeconnect.mainactivity$asynctest.doinbackground(mainactivity.java:63)         @ br.com.dqsoft.testeconnect.mainactivity$asynctest.doinbackground(mainactivity.java:1)         @ android.os.asynctask$2.call(asynctask.java:287)         @ java.util.concurrent.futuretask.run(futuretask.java:234)         ... 4 more     caused by: java.net.sockettimeoutexception: failed connect /192.168.0.13 (port 4040) after 90000ms         @ libcore.io.iobridge.connecterrno(iobridge.java:176)         @ libcore.io.iobridge.connect(iobridge.java:112)         @ libcore.io.iobridge.connect(iobridge.java:100)         ... 9 more  

a not find way catch exception, app closed os. while timeout not normal condition, should able detect , present error message user instead of crashing.

the case connect succeeds works fine in devices.

edit: initial paragraph erroneously mentioned android 2.4 instead of 4.2.2.

edit: simplified code suggest jarred , added additional information.

update: tested on android 4.4.4 device , worked. close bug of android 4.2.2 (maybe on specific device).

remove of this:

you starting thread thread form thread. start single thread main thread, using asynctask.

protected view.onclicklistener dotest = new view.onclicklistener() {     @override     public void onclick(view v) {         new thread(new threadtest()).start();     } };  private class threadtest implements runnable {     @override     public void run() {         try {             runonuithread(new runnable() {                 @override                 public void run() {                     new asynctest().execute();                 }             });         } catch (exception e) {             log.e(tag, e.getmessage());         }     } } 

change to:

protected view.onclicklistener dotest = new view.onclicklistener() {     @override     public void onclick(view v) {         new asynctest().execute();     } }; 

for exception , timeout:

ioexception parent class of sockettimeoutexception, asynctask code should work. when in doubt, can catch using exception.

other things check:

  • can ping 192.168.0.13?
  • how know port open? try telnet or netcat.
  • do have service running , configured correctly on port?
  • do have correct android permissions?

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -