【问题标题】:how to run multiple URL connections?如何运行多个 URL 连接?
【发布时间】:2018-09-23 15:20:17
【问题描述】:

我想使用 Android Native 运行两个连接:

 public class MyPublicClass extends AppCompatActivity {

这是第一堂课

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

这是第二课

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

但是当我运行我的代码时它给了我

2 1

我怎样才能得到

1 2

这意味着在运行GetLibelleOfQuestion之前执行GetNextQuestionIndex的运行

【问题讨论】:

  • 你如何为每个任务调用execute( )

标签: android multithreading url


【解决方案1】:
onCreateActivity(...){
    ...
    ShowDialog();
    AsyncTask1(...).execute();
}

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

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

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

希望对你有帮助。

【讨论】:

  • 我不知道。但检查这篇文章。它可能会有所帮助:stackoverflow.com/questions/3606505/…
  • 我在我的 onPOstExecute 方法中添加了一个参数,它现在可以工作了谢谢 protected void onPostExecute(Void result)
【解决方案2】:

这是调用 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 方法调用或按钮点击,你想要的地方

new GetNextQuestionIndex ().execute("");

【讨论】:

    【解决方案3】:

    您可以使用设置第二个线程休眠片刻(等待第一个线程执行):

    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
    

    【讨论】:

    • 如果第一个异步任务超过 500 毫秒,则睡眠不是一个可行的解决方案,这不起作用
    • 我提出了这个想法,无论如何它可能很有用,因为问题不是很详细。因此,如果我们知道说明所用的时间不会超过一定时间,那么这个解决方案就会起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多