我使用了 Retrofit2 进行文件下载。但我无法使用进度值更新 ProgressBar。我已经获取到进度值,因此没有问题。但是,当我将进度值设置为 ProgressBar 时,UI 上并未反映出来。
我在谈论 RecyclerView Adapter 内部存在的 ProgressBar。
以下是我的 Retrofit 调用, 当在 RecyclerView 中点击项目时,会调用该方法。
private void downloadFileFromServer(String otpapi, String userName, String password, String code, String vmFileName, String filePath, String vmFileSize, int position, CircularProgressBar circularProgress) {
GetDataForApiCall getDataForApiCall= RetrofitInstance.getRetrofit(url,otpapi,context).create(GetDataForApiCall.class);
Call<ResponseBody> downloadVoicemail= getDataForApiCall.downloadVoiceMail(userName,password,code,vmFileName);
this.circularProgressBar=circularProgress;
this.circularProgressBar.setIndeterminate(false);
this.circularProgressBar.setProgress(0);
this.circularProgressBar.setMax(100);
this.circularProgressBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AndroidLogger.log(5,"onClick","circularProgressBar onClick executed!!");
Toast.makeText(context,"cancel clicked",Toast.LENGTH_LONG).show();
cancelDownload = true;
}
});
downloadVoicemail.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
boolean downloadResult = writeResponseBodyToDisk(response.body(),vmFileSize,filePath);
if(downloadResult) {
Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show();
updateVoiceMailFilePath(position, filePath);
updateViews(position);
}else {
deleteVoiceMailFileFromLocalSystem(filePath);
updateViews(position);
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
private boolean writeResponseBodyToDisk( ResponseBody body, String fileSize, String filePath) {
try {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
byte[] fileReader = new byte[8192];
//long fileSize = body.contentLength();
long fileSizeDownloaded = 0;
long lengthOfFile = Long.parseLong( String.format( "%.0f",Double.parseDouble(fileSize )) ) * 1024;
AndroidLogger.log(5,TAG,"filesize"+fileSize + "length of file"+lengthOfFile);
inputStream = body.byteStream();
outputStream = new FileOutputStream(filePath);
while (true) {
int read = inputStream.read(fileReader);
if(cancelDownload){
inputStream.close();
return false;
}
if (read == -1) {
AndroidLogger.log(5,TAG,"-1 value so break");
break;
}
outputStream.write(fileReader, 0, read);
fileSizeDownloaded += read;
if(lengthOfFile >0) {
AndroidLogger.log(5,TAG,"FileSize downloaded"+ fileSizeDownloaded);
int progress = (int) (fileSizeDownloaded * 100 / lengthOfFile);
AndroidLogger.log(5,TAG,"Length of file"+ lengthOfFile);
AndroidLogger.log(5,TAG,"Progress"+ progress);
this.circularProgressBar.setProgress(progress);
update(progress);
}
AndroidLogger.log(5,TAG, "file download: " + fileSizeDownloaded + " of " + fileSize);
}
outputStream.flush();
return true;
} catch (IOException e) {
return false;
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
} catch (IOException e) {
return false;
}
}
我尝试使用Listener更新值,因为Retrofit调用是在其他线程上完成的。因此,我使用了Listener来更新UI,但没有帮助。
我正在使用Retrofit2进行API调用。因此,为了在所有活动中更新UI,我使用了接口监听器。这对所有活动都有效。但是当我在RecyclerView Adapter类中尝试相同的操作时,无法更新进度条。在调用api之前,我将进度条设置为0且最大为100。
以下情况可以正常工作,
循环进度条在API调用之前设置为零
下载完成后,循环进度条将更改为一个勾号
以下情况不起作用,
具有加载指示的循环进度条
注意:只有在使用Retrofit2进行API调用时才遇到此问题。如果我在异步任务内部使用普通的HTTPUrlConnection进行API调用,则进度加载正常工作。
通过以下代码检查了进度更新是否发生在主线程上:
if(Looper.myLooper() == Looper.getMainLooper()) {
circularProgressBar.setProgress(progress);
}
以上的 if 条件已满足,尽管进度条未更新。
此外,我已经尝试了以下方法:
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
AndroidLogger.log(5,TAG,"Running on UI thread");
circularProgressBar.setProgress(progress);
}
};
mainHandler.post(myRunnable);
}
我将这段代码放入了writeResponseBodyToDisk方法中的while循环中,但它只被调用了两次,进度条没有更新。
我注释掉了进度条加载会变成一个勾号的部分。之后当我尝试下载时,下载完成后才能在进度条中看到100%下载完成。在更新进度百分比之前,进度没有反映。
请有人帮助我解决这个问题。
提前致谢。
UIUpdater
接口并覆盖其中相同的方法。如果有帮助的话,你可能需要修改接口以添加一个额外的位置参数来与RecyclerView
一起使用。 - Reaz Murshed