Search code examples
androidandroid-asynctask

AsyncTask - wait for execution of other Asynctask


I was trying to make a program that will get a data with AsyncTask by HttpURLConnection, but in order to get the datas I need to be logged in, so I need to login and then take the data. In order to do have a good OOP, I mustn't have made similar programs that will do the same things. How can I approach this? This is an example of what I did:

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


Solution

  • You can override your onPostExecute function while creating AsyncTasks

    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");