android 中带有 AsyncTask 的两个依赖 HttpUrlConnection

two dependent HttpUrlConnection with AsyncTask in android

我正在使用 android 开发应用程序,我是新手 在我的项目的一部分中,我需要从用户的输入字段中获取数据,然后创建一个到服务器的 Http 连接,并将第一次调用的结果与其他一些字段一起放入另一个到服务器的 Http 连接中,我不知道实施它的正确方法是什么。

public class TwoHttpActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_transport);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}
public static class PlaceholderFragment extends Fragment {

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

         button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                params[0] = firstText;
                .   .   .   .
                .   .   .   .
                params[3] = secondText;
            }
        });
     }
}

问题: 现在我需要将 params[0] 发送到一个 url http://myserver/urlOne and then get the result from the server and put them along with params[1] to params[3] and send them together to the second url of server http://myserver/urlTwo

由于这些连接应该在 classes 内进行,从 AsyncTask 扩展,所以我认为不可能只有

public class FirstUrlTask extends AsyncTask<String[], Void, Void> {
  //the class handles the call to first url inside
  //doInBackground(String[]... params) 
}

public class SecondUrlTask extends AsyncTask<String[], Void, Void> {
       //the class handles the call to second url inside
  //doInBackground(String[]... params) 
    }

然后是

 firstUrlTask.execute(..); 
 secondUrlTask.execute(..);

里面button.setOnClickListener(new View.OnClickListener()

由于它们是异步任务,因此不清楚哪些任务将首先执行,所以请用正确的方法帮助我做到这一点,请提供您的答案并提供详细信息代码示例。

根据我的理解,您需要调用 FirstUrlTask 然后它将从服务器检索数据,之后您需要将这些数据传递到 SecondUrlTask 以执行另一个操作,这取决于从第一个 asyncTask 检索到的数据,您只需将第一个任务调用到 onClickListener() 中,然后在第一个

onPostExecute() 方法中调用第二个

这样做:

第一步:异步调用 FirstUrlTask​​ Class

firstUrlTask.execute(..); 

第二步:在 FirstUrlTask​​ 的 onPostExecute() 中调用 SecondUrlTask​​Async Class

public class FirstUrlTask extends AsyncTask<String[], Void, Void> {

  //doInBackground(String[]... params) {}  

   protected void onPostExecute(ClassTypeYouReturning result){
      super.onPostExecute();
      secondUrlTask.execute(..);   //Call to second url 
   }
}

上述方法将确保第一个调用将对第一个 url 进行,并且在第一个 url 的响应上,将调用第二个 url。

根据要下载的数据,您只能有一个 AsyncTask(如果 2 个任务不会花费很长时间)。请参阅下面 AsyncTask 的代码片段。要将值传递给 AsyncTask,您可以将其作为 arguments 传递,例如 your_async_task.exectue(your_array_of_strings),也可以使用 constructor 传递您的值。

class SingleAsyncTask extends AsyncTask<String, Void, Void> {

    final String[] params;

    public SingleAsyncTask() {
        // default constructor
    }

    public SingleAsyncTask(String... params) {
        this.params = params;
    }

    // the class handles the call to first url inside
    // doInBackground(String[]... params)

    @Override
    protected Void doInBackground(String... params) {
        if (params != null && params.length > 0) {
            // #STEP 1
            String param0 = params[0];
            // make request to 1st URL http://myserver/urlOne

            // #STEP 2
            // when you get a success for 1st URL fire the request for 2nd URL
            // I consider it as 3 as per your problem statement but you can tune
            // as it suites you
            if (params.length > 3) {
                String param1 = params[1];
                // and so on
                // fire request for http://myserver/urlTwo along with result you obtained in step1
            }
        }

        // but try-catch blocks for above statements, as per exceptions you
        // think you need to handle and return final result for "onPostExecute"


        // default return could be null
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // notify your UI
    }

}

希望对您有所帮助!