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



我正在开发应用程序,以便在pdfView中加载pdf文件。我用的是Aynctsask。我想在进度对话框中显示进度状态。

现在我面临的问题是,当我将AsyncTask<String, Void, InputStream>更改为AsyncTask<String, String, InputStream>时,百分比进度运行良好,但pdf文件没有加载到pdfView。

如果我保持AsyncTask<String, Void, InputStream>的原样,我将得到Error。无法解析publishProgress,AsyncTask中的java.lang.Void无法应用于java.lang.Strings。如果我删除publishProgression,我的代码运行良好。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();
}
}

使用<String, Integer, InputStream>并传递Integer而不是String:

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

最新更新