Void中的publishProgress不能应用于Asyntask中的字符串?
我正在开发应用程序来在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
并传递Integer
而不是String
:Use
<String, Integer, InputStream>
and pass anInteger
instead of aString
: