AsyncTask - 等待其他 Asynctask 的执行
AsyncTask - wait for execution of other Asynctask
我试图制作一个程序,通过 HttpURLConnection 使用 AsyncTask 获取数据,但为了获取数据,我需要登录,所以我需要登录然后获取数据。为了拥有一个好的 OOP,我一定不能制作会做同样事情的类似程序。我该如何处理?这是我所做的一个例子:
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("content-type","application/json");
httpURLConnection.setRequestProperty("user-agent","armon-api-android-client");
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
bufferedWriter.write(requestParams.toString());
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
.
.
.
您可以在创建 AsyncTasks 时覆盖您的 onPostExecute 函数
loginAndGetData = new RawRequest(){
@Override
protected void onPostExecute(String res){
//DO YOUR JOB
RawRequest newTask = new RawRequest();
newTask.execute("get data params");
}
loginAndGetData.execute("login params");
我试图制作一个程序,通过 HttpURLConnection 使用 AsyncTask 获取数据,但为了获取数据,我需要登录,所以我需要登录然后获取数据。为了拥有一个好的 OOP,我一定不能制作会做同样事情的类似程序。我该如何处理?这是我所做的一个例子:
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("content-type","application/json");
httpURLConnection.setRequestProperty("user-agent","armon-api-android-client");
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
bufferedWriter.write(requestParams.toString());
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
.
.
.
您可以在创建 AsyncTasks 时覆盖您的 onPostExecute 函数
loginAndGetData = new RawRequest(){
@Override
protected void onPostExecute(String res){
//DO YOUR JOB
RawRequest newTask = new RawRequest();
newTask.execute("get data params");
}
loginAndGetData.execute("login params");