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

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

2023年7月13日发(作者:)

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

OKHttp3是如今非常流行的Android网络请求框架,那么如何利用Android实现断点续传呢,今天写了个Demo尝试了一下,感觉还是有点意思

准备阶段

我们会用到OKHttp3来做网络请求,使用RxJava来实现线程的切换,并且开启Java8来启用Lambda表达式,毕竟RxJava实现线程切换非常方便,而且数据流的形式也非常舒服,同时Lambda和RxJava配合食用味道更佳

打开我们的app Module下的,代码如下

[plain] view plain copy 在CODE上查看代码片派生到我的代码片

apply plugin: 'ation'

android {

compileSdkVersion 24

buildToolsVersion "24.0.3"

defaultConfig {

applicationId "mo"

minSdkVersion 15

targetSdkVersion 24

versionCode 1

versionName "1.0"

testInstrumentationRunner "dJUnitRunner"

//为了开启Java8

jackOptions{

enabled true;

}

}

buildTypes {

release {

minifyEnabled false

proguardFiles getDefaultProguardFile(''), ''

}

}

//开启Java1.8 能够使用lambda表达式

compileOptions{

sourceCompatibility N_1_8

targetCompatibility N_1_8

}

}

dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])

androidTestCompile('so:espresso-core:2.2.2', {

exclude group: 't', module: 'support-annotations'

})

compile 't:appcompat-v7:24.1.1'

testCompile 'junit:junit:4.12'

//OKHttp

compile '3:okhttp:3.6.0'

//RxJava和RxAndroid 用来做线程切换的

compile '2:rxandroid:2.0.1'

compile '2:rxjava:2.0.1'

}

OKHttp和RxJava,RxAndroid使用的都是最新的版本,并且配置开启了Java8

布局文件

接着开始书写布局文件

[html] view plain copy 在CODE上查看代码片派生到我的代码片

xmlns:tools="/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:orientation="vertical"

tools:context="tivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/main_progress1"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="match_parent"

style="@style/ntal" />

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信