Android Rxjava+Okhttp+Retrofit实现单文件下载功能
发布日期:2021-07-01 00:00:24
浏览次数:2
分类:技术文章
本文共 6443 字,大约阅读时间需要 21 分钟。
前言
最近接到一个需求,做一个.xls
文件下载功能 方便用户统计数据
已封装工具类 基于Android Rxjava+Okhttp+Retrofit
实现效果
需要用的工具类如下 以封装完毕 使用简单
FileDownLoadObserver
public abstract class FileDownLoadObserverextends DefaultObserver { public abstract void onDownLoadStart(); //下载成功的回调 public abstract void onDownLoadSuccess(T t); //下载失败回调 public abstract void onDownLoadFail(Throwable throwable); //下载进度监听 public abstract void onDownLoadProgress(int progress, long total); @Override protected void onStart() { super.onStart(); onDownLoadStart(); } @Override public void onNext(T t) { onDownLoadSuccess(t); } @Override public void onError(Throwable e) { onDownLoadFail(e); } @Override public void onComplete() { } public File saveFile(ResponseBody responseBody, String destFileDir, String destFileName) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len = 0; FileOutputStream fos = null; try { is = responseBody.byteStream(); final long total = responseBody.contentLength(); long sum = 0; File dir = new File(destFileDir); if (!dir.exists()) { dir.mkdirs(); } File file = new File(dir, destFileName); fos = new FileOutputStream(file); while ((len = is.read(buf)) != -1) { sum += len; fos.write(buf, 0, len); final long finalSum = sum; //这里就是对进度的监听回调 onDownLoadProgress((int) (finalSum * 100 / total), total); } fos.flush(); return file; } finally { try { if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } } }}
DownLoadUtil 工具
public static String default_path = Environment.getExternalStorageDirectory(). getAbsolutePath() + "/download"; public static void downLoad(String url, String path, String fileName) { downLoad(url, path, fileName, new FileDownLoadObserver() { @Override public void onDownLoadStart() { //开始 } @Override public void onDownLoadSuccess(File file) { //成功 } @Override public void onDownLoadFail(Throwable throwable) { //失败 } @Override public void onDownLoadProgress(int progress, long total) { //下载进度 } }); } public static void downLoad(String url, final String path, final String fileName, final FileDownLoadObserver fileDownLoadObserver) { Retrofit retrofit = new Retrofit.Builder() .client(new OkHttpClient()) .baseUrl(URL_DEFAULT) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); retrofit.create(ApiService.class) .downLoadFile(url) .subscribeOn(Schedulers.io())//subscribeOn和ObserOn必须在io线程,否则出错 .observeOn(Schedulers.io()) .observeOn(Schedulers.computation())//需要 .map(new Function () { @Override public File apply(@NonNull ResponseBody responseBody) throws Exception { return fileDownLoadObserver.saveFile(responseBody, default_path + path, fileName); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(fileDownLoadObserver); }
使用方法
long currentTime = System.currentTimeMillis(); Date date = new Date(currentTime); SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日"); String url = "http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4"; //点击了文件下载 DownLoadUtil.downLoad(url, "", formatter.format(date) + "订单报表.mp4", new FileDownLoadObserver() { @Override public void onDownLoadStart() { progressBar.setVisibility(View.VISIBLE); } @Override public void onDownLoadSuccess(File file) { progressBar.setVisibility(View.GONE); ToastHelper.showToast(OrderManagerActivity.this, "下载完成"); mBtnGoLookFile.setVisibility(View.VISIBLE); mBtnGoLookFile.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); Uri contentUri = FileProvider.getUriForFile (OrderManagerActivity.this, "com.jk.house", file); intent.setDataAndType(contentUri, "*/*"); intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); intent.addCategory(Intent.CATEGORY_OPENABLE); startActivity(intent); mBtnGoLookFile.setVisibility(View.GONE); } }); } @Override public void onDownLoadFail(Throwable throwable) { progressBar.setVisibility(View.GONE); ToastHelper.showToast(OrderManagerActivity.this, "文件下载失败,请重试"); Log.e(TAG, " down load error == >> " + throwable.getMessage()); } @Override public void onDownLoadProgress(int progress, long total) { runOnUiThread(new Runnable() { @Override public void run() { progressBar.setMax(100); progressBar.setProgress(progress); } }); } });
NumberProgressBar
Github地址
在使用过程中 可能会遇到线程的问题 建议参考
共勉
2021 继续努力!~
转载地址:https://lvshichunqiu.blog.csdn.net/article/details/112790172 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月29日 22时58分24秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
如何解决电脑ip地址冲突的问题
2019-05-01
Win下如何查看本地计算机的网络端口被哪个应用程序所占用
2019-05-01
TCP/IP、Http、Socket的区别
2019-05-01
Java网络聊天程序
2019-05-01
Java常用类和方法重点总结
2019-05-01
Java高级部分容器重点总结上
2019-05-01
虚拟机与Linux操作系统的安装步骤
2019-05-01
Java高级部分容器重点总结下
2019-05-01
Java高级部分流重点总结上
2019-05-01
Java高级部分流重点总结下
2019-05-01
Java高级部分网络编程重点总结
2019-05-01
Java高级部分组件重点总结
2019-05-01
Java高级部分之线程重点总结(上)
2019-05-01
Java高级部分之线程重点总结(下)
2019-05-01
MapReduce程序运行流程
2019-05-01
MapReduce常见算法
2019-05-01
Eclipse中JVM内存配置
2019-05-01
Hadoop 新 MapReduce 框架 Yarn 详解
2019-05-01
Hadoop开发---经验总结1
2019-05-01