0
public class SecondActivity2 extends AppCompatActivity {

    public static String main_html = "";
    public static int second_action = 0;
    WebView webView2;
    public static taskResearch ndc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        webView2 = (WebView) findViewById(R.id.webView);

        String userid = "12345";

        try {
            ndc = new TaskResearch(userid, "1", "keyword", 5, 4, "orderid", 6, "title", "text");
            ndc.execute("");
        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.v("htmlg", "taskResearch finished");

    }



    class TaskResearch  extends AsyncTask <String, String, String> {

        String deviceId, s_id, keyword, city, s_title, s_text;
        int count1, size1, online;

        public TaskResearch(String deviceId, String s_id, String keyword, int count1, int size1, String city, int online, String s_title, String s_text){

            this.deviceId = deviceId;
            this.s_id = s_id;
            this.keyword = keyword;
            this.city = city;
            this.s_title = s_title;
            this.s_text = s_text;
            this.count1 = count1;
            this.size1 = size1;
            this.online = online;

        }

        protected String doInBackground(String... strings) {

            String sonuc = "";

            runOnUiThread(new Runnable() {
                public void run() {





                }
            });

            return "a";

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }


    }

    class html_reader {

        @SuppressWarnings("unused")
        @JavascriptInterface
        public void processHTML(final String html) {

            // Log.d("HTMLG", "html: " + html);

            // Log.d("HTMLG", "html: " + html);
            // html_reader jssil = new html_reader(deviceId, sId,  swAfter, swcity);

            // webView2.addJavascriptInterface(jssil, "HTMLOUT");

            main_html = html;

        }

        @SuppressWarnings("unused")
        @JavascriptInterface
        public void ok() {

            // Log.d("HTMLG", "html: " + html);

            // Log.d("HTMLG", "html: " + html);
            // html_reader jssil = new html_reader(deviceId, sId,  swAfter, swcity);

            // webView2.addJavascriptInterface(jssil, "HTMLOUT");

            second_action = 1;

        }


}

Hello Friends.

My codes are above. I want to run this command.

ndc = new taskResearch(userid, "1", "keyword", 5, 4, "orderid", 6, "title", "text");

When this command finished. I want this command to run.

Log.v("htmlg", "taskResearch finished");

But 2 of them start together and second one finishes firstly. I want that first command should start and finish then second command should start. How can i do that ?

What is my mistake ?

BadZen
  • 4,083
  • 2
  • 25
  • 48
  • Possible duplicate of [Android calling AsyncTask right after an another finished](http://stackoverflow.com/questions/10048958/android-calling-asynctask-right-after-an-another-finished) – Talha Nov 06 '16 at 07:03

2 Answers2

0

By the way, If you are fecthing html code from a website, you can use different structures.

You can use onPostExecute to detect process end.

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    processFinished();
}


void processFinished(){
   Log.v("htmlg", "task_research finished");
}
EmreSURK
  • 419
  • 3
  • 13
0

One way to do it is override the onPostExecute method, as mentioned in another post. Another way to do it is as follows:

After you call ndc.execute() you can wait for it to get a result like this:

String output = ndc.get(); // i believe this is the api

Once you are returned that output, you can do whatever you want to it and the second log will print after.

paul_hundal
  • 371
  • 2
  • 9