According to The Docs
Threading rules
There are a few threading rules that must be followed for this class to work properly:
The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN. The task instance must be created on the UI thread.
You can handle this by calling your new Thread
in the onPostExecute()
of your first AsyncTask
after your accept()
is successful, as onPostExecute()
runs on the UI
Also, without looking at it more, I believe that you would want to break out of your while loop
in the first task after it accepts the request. Then when you need to make another connection you create a new instance of this task in your UI
and execute
it again. I'm not positive about that last part without looking at it longer but that seems right