Void中的publishProgress不能应用于Asyntask中的字符串?

发布于 2025-01-13 20:18:45 字数 2073 浏览 1 评论 0原文

我正在开发应用程序来在 pdfView 中加载 pdf 文件。我正在为此使用 Aynctask。我想在进度对话框中显示进度状态。

现在我面临的问题是,当我将 AsyncTask 更改为 AsyncTask 进度百分比效果很好,但 pdf 文件不行正在加载到 pdfView。

如果我按原样保留 AsyncTask ,则会收到错误无法解析publishProgress,AsyncTask中的java.lang.Void无法应用于java.lang.Strings。如果我删除publishProgress,我的代码运行良好,pdf文件正在加载到pdfView,但进度对话框中没有实际的进度更新。 如何修复它?请帮助我,我被困在这里很多天了。

代码

这是我的PdfViewer.java

class RetrievePdfStream extends AsyncTask<String, Void, InputStream> {
        @Override
        protected InputStream doInBackground(String... strings) {
            InputStream inputStream = null;
            int count;
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                int lenghtOfFile = urlConnection.getContentLength();
                if (urlConnection.getResponseCode() == 200) {
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = inputStream.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                  publishProgress(""+(int)((total*100)/lenghtOfFile));
                }
            }
            catch (IOException e) {
                return null;
            }
            return inputStream;
        }

        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        protected void onPostExecute(InputStream inputStream) {
            pdf.fromStream(inputStream).load();
            pDialog.cancel();
        }
    }

I'm developing app to load pdf files in pdfView. I'm using Aynctask for this. I want to show the progress status in progress dialog.

Now the problem I'm facing is when I change AsyncTask<String, Void, InputStream> to AsyncTask<String, String, InputStream> percentage progress works well but pdf file not getting loaded to pdfView.

If I leave AsyncTask<String, Void, InputStream> as it is, I'm getting Error cannot resolve publishProgress, java.lang.Void in AsyncTask cannot be applied to java.lang.Strings. If I remove publishProgress my code works well pdf file is loading to pdfView but there is no actual progress update in progress dialog.
How to fix it? Please help me, I'm stuck here from many days.

here is my code for

PdfViewer.java

class RetrievePdfStream extends AsyncTask<String, Void, InputStream> {
        @Override
        protected InputStream doInBackground(String... strings) {
            InputStream inputStream = null;
            int count;
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                int lenghtOfFile = urlConnection.getContentLength();
                if (urlConnection.getResponseCode() == 200) {
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());
                }
                byte data[] = new byte[1024];
                long total = 0;
                while ((count = inputStream.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    // After this onProgressUpdate will be called
                  publishProgress(""+(int)((total*100)/lenghtOfFile));
                }
            }
            catch (IOException e) {
                return null;
            }
            return inputStream;
        }

        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        protected void onPostExecute(InputStream inputStream) {
            pdf.fromStream(inputStream).load();
            pDialog.cancel();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2025-01-20 20:18:45

使用 并传递 Integer 而不是 String

publishProgress((int)((total*100)/lenghtOfFile))

Use <String, Integer, InputStream> and pass an Integer instead of a String:

publishProgress((int)((total*100)/lenghtOfFile))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文