Получение вывода AsyncTask в другой класс - PullRequest
1 голос
/ 03 мая 2020

Я изучаю android разработка. Мне нужно проверить, присутствует ли файл на сервере.

Я использую следующий код

public class CheckReportExists extends AsyncTask<String, Void, Boolean>{

    Boolean fileExists = false;

    public CheckReportExists() {

    }

    public Boolean CheckReportExists1(String download_url){
        execute(download_url);
        return fileExists;
    }

    @Override
    protected void onPreExecute() {
        //display progress dialog.

    }
    @Override
    protected Boolean doInBackground(String... params) {
        try {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection con =  (HttpURLConnection) new URL(params[0]).openConnection();
            con.setRequestMethod("HEAD");
            int response = con.getResponseCode();
            if(response == HttpURLConnection.HTTP_OK){
                fileExists = true;
            }
        } catch(Exception e){

        }
        return fileExists;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // dismiss progress dialog and update ui
        //super.onPostExecute(result);
    }

}

Для вызова я использую следующий код

     CheckReportExists cre = new CheckReportExists();
     Boolean fileExists = cre.CheckReportExists1(download_url);
     if(fileExists) {
          builder.setPositiveButton("Download Report", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
                 new DownloadTask(Results.this, download_url);
          }
          });
      }else{
            builder.setPositiveButton("Report not ready yet", new DialogInterface.OnClickListener() {
                   @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
             });
      }

Но этот код не работает, время я получаю AlertDialog с кнопкой «Отчет еще не готов», даже если файл присутствует на сервере.

Спасибо.

Ответы [ 2 ]

0 голосов
/ 03 мая 2020

CheckReportExists1() всегда будет возвращать значение false; и это потому, что он работает в потоке пользовательского интерфейса и не будет ждать, пока AsyncTask не завершит свою фоновую работу. Таким образом, он всегда будет возвращать свое начальное присвоенное значение, равное false

. Чтобы решить эту проблему, вы можете создать интерфейс слушателя с методом обратного вызова, который принимает Boolean, который указывает, существует файл или нет. И запускайте этот обратный вызов всякий раз, когда вы действительно убедитесь, что файл существует или нет после завершения фоновой работы.

Поэтому измените код с помощью:

Интерфейс слушателя:

interface OutputListener {
    void checkFile(Boolean exists);
}

AsyncTask:

public class CheckReportExists extends AsyncTask<String, Void, Boolean>{

    Boolean fileExists = false;
    OutputListener listener;

    public CheckReportExists() {

    }

    public void CheckReportExists1(String download_url, OutputListener outputListener){
        execute(download_url);
        listener = outputListener;
    }

    @Override
    protected void onPreExecute() {
        //display progress dialog.

    }
    @Override
    protected Boolean doInBackground(String... params) {
        try {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection con =  (HttpURLConnection) new URL(params[0]).openConnection();
            con.setRequestMethod("HEAD");
            int response = con.getResponseCode();
            if(response == HttpURLConnection.HTTP_OK){
                fileExists = true;
            }
        } catch(Exception e){

        }
        return fileExists;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (listener != null)
            listener.checkFile(result);

        // dismiss progress dialog and update ui
        //super.onPostExecute(result);
    }

}

Использование AsyncTask:

CheckReportExists cre = new CheckReportExists();
cre.CheckReportExists1(download_url, new OutputListener() {
            @Override
            public void checkFile(Boolean exists) {
                if(fileExists) {
                    builder.setPositiveButton("Download Report", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                             new DownloadTask(Results.this, download_url);
                        }
                    });
                } else {
                    builder.setPositiveButton("Report not ready yet", new DialogInterface.OnClickListener() {
                           @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                     });
                }

            }
        });
0 голосов
/ 03 мая 2020

Примерно так:

 CheckReportExists cre = new CheckReportExists() {
     @Override
     protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        // RIGHT HERE is where you can add your code to handle the result
     }
 };
 // CheckReportExists1 is no longer a good name because it's not
 // returning a value.  It just starts the process.
 cre.CheckReportExists1(download_url);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...