show how to use retrofit download file with progress
-
clone the library and include it to your project
-
set okHttpClick(optional)
if you want to set yourself okHttpClick, use following config
DownloadUtil.getInstance().initConfig(OkHttpClient.Builder);
-
invoke
DownloadUtil.getInstance() .downloadFile(new InputParameter.Builder(baseUrl, relativeUrl, downloadedFilePath) .setCallbackOnUiThread(true) .build(), new DownloadListener() { @Override public void onFinish(final File file) { //you can let this callback run on UI thread by setCallbackOnUiThread(true) in inputParameter } @Override public void onProgress(int progress, long downloadedLengthKb, long totalLengthKb) { tvProgress.setText(String.format("文件文件下载进度:%d%s \n\n已下载:%sKB | 总长:%sKB", progress,"%", downloadedLengthKb + "", totalLengthKb + "")); } @Override public void onFailed(String errMsg) { //you can let this callback run on UI thread by setCallbackOnUiThread(true) in inputParameter } });
note: The
onFinish()
andonFailed()
methods of downloadListener are run on the non-ui thread by default, you can change it bysetCallbackOnUiThread(true)
of inputParameter