介绍
这篇文章给大家介绍在Android项目中使用OKHttp3怎么实现一个下载功能,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
应用模块下的构建。gradle,代码如下
应用插件:& # 39;com.android.application& # 39; 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.AndroidJUnitRunner"//为了开启Java8 jackOptions { 启用正确; } } buildTypes { 释放{ minifyEnabled假 proguardFiles getDefaultProguardFile (& # 39; proguard-android.txt& # 39;), & # 39; proguard-rules.pro& # 39; } }//开启Java1.8能够使用λ表达式 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } 依赖关系{ 编译fileTree (dir: & # 39;填词# 39;,包括:[& # 39;* . jar # 39;]) androidTestCompile (& # 39; com.android.support.test.espresso: espresso-core: 2.2.2 # 39;, { 排除组:& # 39;com.android.support& # 39;模块:& # 39;support-annotations& # 39; }) 编译& # 39;com.android.support: appcompat-v7:24.1.1& # 39; testCompile & # 39; junit: junit: 4.12 & # 39;//OKHttp 编译& # 39;com.squareup.okhttp3: okhttp: 3.6.0& # 39;//RxJava和RxAndroid用来做线程切换的 编译& # 39;io.reactivex.rxjava2: rxandroid: 2.0.1 # 39; 编译& # 39;io.reactivex.rxjava2: rxjava: 2.0.1 # 39; }
OKHttp和RxJava, RxAndroid使用的都是最新的版本,并且配置开启了Java8
接着开始书写布局文件
& lt;及# 63;xml version=?.0”;编码=皍tf-8", # 63;比; http://schemas.android.com/apk/res/android" & lt; LinearLayout xmlns: android=?; xmlns:工具=癶ttp://schemas.android.com/tools" android: id=癅 + id/activity_main" android: layout_width=癿atch_parent" android: layout_height=癿atch_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:取向=皏ertical" 工具:上下文=癱om.lanou3g.downdemo.MainActivity"比; LinearLayout & lt; android: layout_width=癿atch_parent" android: layout_height=皐rap_content" android:取向=癶orizontal"比; & lt; ProgressBar android: id=癅 + id/main_progress1" android: layout_width=? dp" android: layout_weight=?”; android: layout_height=癿atch_parent"/比; & lt;按钮 android: id=癅 + id/main_btn_down1" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android:文本=跋略?“/比; & lt;按钮 android: id=癅 + id/main_btn_cancel1" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android:文本=叭∠?“/比; & lt;/LinearLayout> LinearLayout & lt; android: layout_width=癿atch_parent" android: layout_height=皐rap_content" android:取向=癶orizontal"比; & lt; ProgressBar android: id=癅 + id/main_progress2" android: layout_width=? dp" android: layout_weight=?”; android: layout_height=癿atch_parent"/比; & lt;按钮 android: id=癅 + id/main_btn_down2" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android:文本=跋略?“/比; & lt;按钮 android: id=癅 + id/main_btn_cancel2" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android:文本=叭∠?“/比; & lt;/LinearLayout> LinearLayout & lt; android: layout_width=癿atch_parent" android: layout_height=皐rap_content" android:取向=癶orizontal"比; & lt; ProgressBar android: id=癅 + id/main_progress3" android: layout_width=? dp" android: layout_weight=?”; android: layout_height=癿atch_parent"/比; & lt;按钮 android: id=癅 + id/main_btn_down3" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android:文本=跋略?“/比; & lt;按钮 android: id=癅 + id/main_btn_cancel3" android: layout_width=皐rap_content" android: layout_height=皐rap_content" android:文本=叭∠?“/比; & lt;/LinearLayout> & lt;/LinearLayout>在Android项目中使用OKHttp3怎么实现一个下载功能