Good day. Tell me what could be the problem. Below is the code that does not work, although it should. The application crashes without any errors. If mResultChekc = 1; I transfer from the onPostExecute () method to the doInBackground () method before bufferedReader.close (); then everything works. Although everywhere they write that the end of AsyncTask is the execution of onPostExecute () and in it you need to set a variable about the end.

Another application crashes if you use StringBuilder instead of String mString. After executing the string mString.append (line) in a loop.

There is a class inherited from AsyncTask:

public class DownloadCountry extends AsyncTask<Void, Void, String>{ private String mString; public int mResultChekc = 0; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(Void... params) { //Подключаемся к сайту и скачиваем содержимое try { URL pageURL = new URL("https:хххххх.com"); URLConnection urlConnection = pageURL.openConnection(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); String line = null; while ((line = bufferedReader.readLine()) != null){ mString = line; } bufferedReader.close(); } catch (Exception e){ e.printStackTrace(); } return mString; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); // Сообщаем о том что все действия выполнены. mResultChekc = 1; } } 

And another class that is waiting for its implementation:

 public class Splash extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.SplashTheme); super.onCreate(savedInstanceState); // Запускаем поток для скачивания // Ждем когда он выполнится и переходим на Мain DownloadCountry dc = new DownloadCountry(); dc.execute(); while (true){ if (dc.mResultChekc == 1) { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); return; } } } } 

    1 answer 1

    You have an ANR application crashing (ApplicationNotResponded) due to the fact that you have frozen the main thread for more than 5 seconds.

    You initially chose the wrong approach. Instead of waiting for the completion of an asynchronous task in an infinite loop of the main thread, you need to wait until it completes and call the onPostExecute method — and execute the code from it.

    If the task class is not described in the activation class, then you can create an interface in the task, implement it in activation and transfer it to the activation task through the task constructor. Then in onPostExecute call the interface method.

    So you will run the code from the activation after the task is completed and without freezing the main thread.

    In general, AsyncTask is outdated and is no longer used because with him a lot of problems. But I think it’s too early to advise RxJava, although you can try

    • RxJava is interesting, but so far with AsyncTask. If ANR crashes then how to properly implement the work? You can clarify what you meant: If the task class is not described in the activation class, then you can create an interface in the task, implement it in the activation and transfer it to the activation task through the task constructor. Then in onPostExecute call the interface method. ? - Anatoly Ferisov
    • @AnatolyFerisov, here's an example of how it should be with AsyncTask th ru.stackoverflow.com/a/373522/17609 - Yuriy SPb
    • Thank you very much, what you need, now everything works. - Anatoly Ferisov
    • @AnatolyFerisov, if my answer helped you, you can mark it "true" by clicking on the check mark to the left of the response body) - YuriySPb
    • Rx huge library ... - Flippy