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; } } } }