android使用okhttp3实现下载(断点续传、显示进度)

上传人:第*** 文档编号:31309898 上传时间:2018-02-06 格式:DOC 页数:19 大小:229KB
返回 下载 相关 举报
android使用okhttp3实现下载(断点续传、显示进度)_第1页
第1页 / 共19页
android使用okhttp3实现下载(断点续传、显示进度)_第2页
第2页 / 共19页
android使用okhttp3实现下载(断点续传、显示进度)_第3页
第3页 / 共19页
android使用okhttp3实现下载(断点续传、显示进度)_第4页
第4页 / 共19页
android使用okhttp3实现下载(断点续传、显示进度)_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《android使用okhttp3实现下载(断点续传、显示进度)》由会员分享,可在线阅读,更多相关《android使用okhttp3实现下载(断点续传、显示进度)(19页珍藏版)》请在金锄头文库上搜索。

1、Android 使用 OKHttp3 实现下载(断点续传、显示进度)OKHttp3 是如今非常流行的 Android 网络请求框架,那么如何利用 Android 实现断点续传呢,今天写了个 Demo 尝试了一下,感觉还是有点意思准备阶段我们会用到 OKHttp3 来做网络请求 ,使用 RxJava 来实现线程的切换 ,并且开启 Java8 来启用Lambda 表达式,毕竟 RxJava 实现线程切换非常方便,而且数据流的形式也非常舒服,同时Lambda 和 RxJava 配合食用味道更佳打开我们的 app Module 下的 build.gradle,代码如下plain view plain

2、copy 在 CODE 上查看代码片派生到我的代码片apply plugin: com.android.application android compileSdkVersion 24 buildToolsVersion 24.0.3 defaultConfig applicationId com.lanou3g.downdemo minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName 1.0 testInstrumentationRunner android.support.test.runner.AndroidJUnit

3、Runner /为了开启 Java8 jackOptions enabled true; buildTypes release minifyEnabled false proguardFiles getDefaultProguardFile(proguard-android.txt), proguard-rules.pro /开启 Java1.8 能够使用 lambda 表达式 compileOptions sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 depend

4、encies compile fileTree(dir: libs, include: *.jar) androidTestCompile(com.android.support.test.espresso:espresso-core:2.2.2, exclude group: com.android.support, module: support-annotations ) compile com.android.support:appcompat-v7:24.1.1 testCompile junit:junit:4.12 /OKHttp compile com.squareup.okh

5、ttp3:okhttp:3.6.0 /RxJava 和 RxAndroid 用来做线程切换的 compile io.reactivex.rxjava2:rxandroid:2.0.1 compile io.reactivex.rxjava2:rxjava:2.0.1 OKHttp 和 RxJava,RxAndroid 使用的都是最新的版本,并且配置开启了 Java8布局文件接着开始书写布局文件html view plain copy 在 CODE 上查看代码片派生到我的代码片 大概是这个样子的3 个 ProgressBar 就是为了显示进度的 ,每个 ProgressBar 对应 2 个 Bu

6、tton,一个是开始下载,一个是暂停(取消)下载,这里需要说明的是,对下载来说暂停和取消没有什么区别 ,除非当取消的时候,会顺带把临时文件都删除了,在本例里是不区分他俩的.Application我们这里需要用到一些文件路径,有一个全局 Context 会比较方便, 而 Application 也是Context 的子类,使用它的是最方便的,所以我们写一个类来继承 Applicationjava view plain copy 在 CODE 上查看代码片派生到我的代码片package com.lanou3g.downdemo; import android.app.Application; im

7、port android.content.Context; /* * Created by 陈丰尧 on 2017/2/2. */ public class MyApp extends Application public static Context sContext;/全局的 Context 对象 Override public void onCreate() super.onCreate(); sContext = this; 可以看到,我们就是要获得一个全局的 Context 对象的我们在 AndroidManifest 中注册一下我们的 Application,同时再把我们所需要的权

8、限给上html view plain copy 在 CODE 上查看代码片派生到我的代码片 我们只需要一个网络权限,在 application 标签下,添加 name 属性,来指向我们的 ApplicationDownloadManager接下来是核心代码了,就是我们的 DownloadManager,先上代码java view plain copy 在 CODE 上查看代码片派生到我的代码片package com.lanou3g.downdemo; import java.io.File; import java.io.FileOutputStream; import java.io.IOE

9、xception; import java.io.InputStream; import java.util.HashMap; import java.util.concurrent.atomic.AtomicReference; import io.reactivex.Observable; import io.reactivex.ObservableEmitter; import io.reactivex.ObservableOnSubscribe; import io.reactivex.android.schedulers.AndroidSchedulers; import io.re

10、activex.schedulers.Schedulers; import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; /* * Created by 陈丰尧 on 2017/2/2. */ public class DownloadManager private static final AtomicReference INSTANCE = new AtomicReference(); private HashMap downCalls;/用来存放各个下

11、载的请求 private OkHttpClient mClient;/OKHttpClient; /获得一个单例类 public static DownloadManager getInstance() for (; ; ) DownloadManager current = INSTANCE.get(); if (current != null) return current; current = new DownloadManager(); if (INSTANCE.compareAndSet(null, current) return current; private DownloadM

12、anager() downCalls = new HashMap(); mClient = new OkHttpClient.Builder().build(); /* * 开始下载 * * param url 下载请求的网址 * param downLoadObserver 用来回调的接口 */ public void download(String url, DownLoadObserver downLoadObserver) Observable.just(url) .filter(s - !downCalls.containsKey(s)/call 的 map 已经有了,就证明正在下载

13、,则这次不下载 .flatMap(s - Observable.just(createDownInfo(s) .map(this:getRealFileName)/检测本地文件夹,生成新的文件名 .flatMap(downloadInfo - Observable.create(new DownloadSubscribe(downloadInfo)/下载 .observeOn(AndroidSchedulers.mainThread()/在主线程回调 .subscribeOn(Schedulers.io()/在子线程执行 .subscribe(downLoadObserver);/添加观察者

14、public void cancel(String url) Call call = downCalls.get(url); if (call != null) call.cancel();/取消 downCalls.remove(url); /* * 创建 DownInfo * * param url 请求网址 * return DownInfo */ private DownloadInfo createDownInfo(String url) DownloadInfo downloadInfo = new DownloadInfo(url); long contentLength = g

15、etContentLength(url);/获得文件大小 downloadInfo.setTotal(contentLength); String fileName = url.substring(url.lastIndexOf(/); downloadInfo.setFileName(fileName); return downloadInfo; private DownloadInfo getRealFileName(DownloadInfo downloadInfo) String fileName = downloadInfo.getFileName(); long downloadLength = 0, contentLength = downloadInfo.getTotal(); File file = new File(MyApp.sContext.getFilesDir(), fileName); if (file.exists() /找到了文

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 其它中学文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号