0

I want to run two connections using Android Native :

 public class MyPublicClass extends AppCompatActivity {

here is the first class

private class GetNextQuestionIndex extends AsyncTask<String, Void, Void> {
    //some code
    protected Void doInBackground(String... params) {
    URL url = new URL("url1");
    //some code to initialize connection and get the output

    MyPublicClass.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mytxtview.setText(output1)
                        System.out.println("1");
                        progress.dismiss();
                    }
                });

Here is the second class

private class GetLibelleOfQuestion extends AsyncTask<String, Void, Void> {
    //some code
    protected Void doInBackground(String... params) {
    URL url = new URL("url2");
    //some code to initialize another connection and get another output

    MyPublicClass.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mytxtview.setText(output2)
                        System.out.println("2");
                        progress.dismiss();
                    }
                });
}//the end of the public class

but when i run my code its give me

2 1

how can I get

1 2

? which means execute the run of GetNextQuestionIndex before the run of GetLibelleOfQuestion

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
betty bth
  • 33
  • 7

3 Answers3

1
onCreateActivity(...){
    ...
    ShowDialog();
    AsyncTask1(...).execute();
}

public void callAsyncTask2(){
        AsyncTask2(...).execute();
}

class AsyncTask1(...){
    ....
    onPostExecute(...){
    activity.callAsyncTask2();
    }
}

class AsyncTask2(...){
    ....
    onPostExecute(...){
    activity.dismissDialog();
    }
}

Hope it helps.

Vignesh KM
  • 1,979
  • 1
  • 18
  • 24
  • I don't have any idea. but check this post. It might help: https://stackoverflow.com/questions/3606505/onpostexecute-not-called-after-completion-asynctask?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Vignesh KM Apr 13 '18 at 10:27
  • I added a parameter to my onPOstExecute method and its work now Thank you protected void onPostExecute(Void result) – betty bth Apr 13 '18 at 10:27
0

You can use set the second thread to sleep for a moment (waiting for the first thread to be executed):

private class GetLibelleOfQuestion extends AsyncTask<String, Void, Void> {
...
    MyPublicClass.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Thread.sleep(WAIT_TIME_IN_MILLISECONDS);
                        mytxtview.setText(output2)
                        System.out.println("2");
                        progress.dismiss();
                    }
                });
}//the end of the public class
Ясир
  • 136
  • 1
  • 16
  • Sleep is not a viable solution if the first async task during more than 500ms, this does not works – Thomas Mary Apr 13 '18 at 10:03
  • I'm popping up this idea that could be useful anyways as the question was not very much detailed. So this solution will work if we know that the instructions will not take longer than an amount of time. – Ясир Apr 13 '18 at 10:16
0

This is the proper ways to call 2 asyn task .

    private class GetNextQuestionIndex extends AsyncTask<String, Void, String> {

                @Override
                protected String doInBackground(String... params) {
                     URL url = new URL("url2");
                   //run your background task
                    return results;
                }

                @Override
                protected void onPostExecute(String result) {
                      mytxtview.setText(output1)
                            System.out.println("1");
                              new GetLibelleOfQuestion ().execute("");
                }

                @Override
                protected void onPreExecute() {
                progress.dismiss();
                }

                @Override
                protected void onProgressUpdate(Void... values) {}
            }
        }

//Second asyn task

      private class GetLibelleOfQuestion extends AsyncTask<String, Void, String> {

                @Override
                protected String doInBackground(String... params) {
                     URL url = new URL("url2");
                   //run your background task
                    return results;
                }

                @Override
                protected void onPostExecute(String result) {
                      mytxtview.setText(output2)
                            System.out.println("2");
                            progress.dismiss();
                }

                @Override
                protected void onPreExecute() {}

                @Override
                protected void onProgressUpdate(Void... values) {}
            }
        }

oncreate method call or button click , where you want

new GetNextQuestionIndex ().execute("");
Praveen Rawat
  • 314
  • 1
  • 3
  • 14