Hello everyone) I have such a task: From the activity I launch the upload of a single JSON-chick on a button via AsynkTask. I need to launch a progress bar, make progressBar.setCanceble(false); until the load happens (successfully). After a successful download, the activity should start, which will already do the parsing values, since I want to, but I do not know how to do it.

Question: where to initialize - new ParseTask().execute(); , to start it as if to say ... Probably, in a separate thread. And so that nothing else is included, you know what I mean ?. For example, a scanner device beacons.

  • one
    The question is incomprehensible. new ParseTask().execute(); - runs the doInBackground() method in a separate thread. - Yuriy SPb
  • Understand, to launch starts, and even the data returns. But not at once. I need to work with the received data. When I did not know, errors fell down, I began to correct, and I realized that the code after the initialisation is performed faster than doInBackground. In some moments, you have to do crutches and wait for this Jason to come. But I would like to work right after it is loaded. Then my project will not fall. Or try to do the download in a separate class or even the screen? Ps I will what is not clear - Egor
  • 2
    In AsynkTask, there is an onPostExecute() method in which it is necessary to respond to the arrival of data. Execute code that requires data immediately after calling new ParseTask().execute(); is impossible. Totally. You need to write logic in order to take this into account - Yuriy SPb

1 answer 1

Indeed, after the execution of lines

 new NetworkClass((ScanningActivity) mContext).execute(); 

the doInBackground() method is doInBackground() on a separate thread.

In NetworkClass , inherited from AsynkTask , the ability to run a progress bar, which is located in ScanningActivity with a special constructor, and a certain Boolean variable that will allow the execution of the necessary code section in the main Activity after downloading data has been added

 public NetworkClass(ScanningActivity scanningActivity) { this.scanningActivity = scanningActivity; } @Override protected void onPreExecute() { super.onPreExecute(); this.scanningActivity.showProgress(); } .... @Override public void onPostExecute(String strJson) { super.onPostExecute(strJson); ... this.scanningActivity.hideProgress(); ScanningActivity.checking = true; } 

The question is closed. As tests have shown, this solves my problem. Thank you for participating in the YuriyuSPB discussions.