0

In this line :

 while ((bytesRead = inputStream.read(content)) != -1) {

This line is inside in astyntask

in logs I see this :

 at java.net.PlainSocketImpl.read(PlainSocketImpl.java:492)
 W/System.err:     at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
 W/System.err:     at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:241)
 W/System.err:     at java.io.InputStream.read(InputStream.java:162)
 W/System.err:     at pl.eltegps.teminalmobile.Activity.MainActivity$connectTask3.doInBackground(MainActivity.java:1353)
 W/System.err:     at pl.eltegps.teminalmobile.Activity.MainActivity$connectTask3.doInBackground(MainActivity.java:1205)
 W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
 W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
 W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
 W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
 W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
 W/System.err:     at java.lang.Thread.run(Thread.java:841)
Krzysztof Pokrywka
  • 1,356
  • 4
  • 27
  • 50

1 Answers1

1

AsyncTask documentation has some threading rules:

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.
  • execute(Params...) must be invoked on the UI thread.
  • Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
  • The task can be executed only once (an exception will be thrown if a second execution is attempted.)

You cannot execute multiple times the same task, only once.

UPDATE

In your "paste.ofcode.org" url it seems that you break 3 rules of the threading rules above (boldly marked)

Rules 2&3 break here, (why start a new thread to execute an AsyncTask?):

thread = new Thread() {
            @Override
            public void run() {
                new connectTask3().execute();
            }
        };
        thread.start();

Do not create a new thread, instead delete the above code and replace it with just:

new connectTask3().execute();
pleft
  • 7,567
  • 2
  • 21
  • 45
  • Ok but I don't use this same object I create a new object and I do execute – Krzysztof Pokrywka Nov 07 '17 at 12:54
  • Can you update your answer and post the relevant code to see how the asynctask is instantiated and executed? – pleft Nov 07 '17 at 12:57
  • Please dont paste urls, update your question and post the code there. The url in your comment is the asynctask itself and it is huge and badly formatted, I cannot check if there is another class inside there, we need to see how it is called in your `MainActivity`. Please update your question and post the code of your `MainActivity` – pleft Nov 07 '17 at 13:00
  • Now I see that you execute multiple times the asynctask from inside `onPostExecute` of the same asynctask via a `TimerTask` and from the last url, you start a new thread to execute the asynctask. If you read my answer rule number 3 says to execute the task from the UI thread, so creating a new thread to execute it doesn't seem correct. Please I will not reply anymore to posting urls, update your question with the relevant pieces of code. thank you – pleft Nov 07 '17 at 13:07
  • Can you tell me how it should look like? – Krzysztof Pokrywka Nov 07 '17 at 13:35
  • For the TImerTask code part check here: https://stackoverflow.com/a/6532298/3635454 For running the task from a new thread, just instantiate and execute the task outside of this thread (I will update my answer in a minute) – pleft Nov 07 '17 at 13:38
  • I replece my TImer to callAsynchronousTask and I dn't create a new thread but sometimes i have this same error – Krzysztof Pokrywka Nov 07 '17 at 13:45
  • Then use a `service` instead of an `AsyncTask` see the comments in the link I provided in my comment above – pleft Nov 07 '17 at 13:57
  • Can I use in service AsynTask ? – Krzysztof Pokrywka Nov 07 '17 at 14:00
  • Just read a tutorial please, see: https://developer.android.com/training/run-background-service/index.html – pleft Nov 07 '17 at 14:02