1.导入okhttp包
implementation 'cn.finalteam:okhttpfinal:2.0.7'
2.单个下载
package com.example.downloading;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.view.View;import java.io.File;import cn.finalteam.okhttpfinal.FileDownloadCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.OkHttpFinal;
import cn.finalteam.okhttpfinal.OkHttpFinalConfiguration;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//先初始化okhttpOkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();OkHttpFinal.getInstance().init(builder.build());findViewById(R.id.Play_videos_and_pictures).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Download("https:...../2_0ee2238f8f8cd7b3773cc6d82dafb756.mp4",new File("/storage/emulated/0/Download/2adfed77da173f.mp4"));}});}public static void Download(final String url, final File saveFile) {Log.e("TAG","url===="+url);Log.e("TAG","saveFile===="+saveFile.getAbsolutePath());HttpRequest.download(url, saveFile, new FileDownloadCallback() {//开始下载@Overridepublic void onStart() {super.onStart();}//下载进度@Overridepublic void onProgress(int progress, long networkSpeed) {super.onProgress(progress, networkSpeed);Log.e("TAG", "进度值是:" + progress);}//下载失败@Overridepublic void onFailure() {super.onFailure();Log.e("TAG", "下载失败的地址是:" + url);}//下载完成(下载成功)@Overridepublic void onDone() {super.onDone();Log.e("TAG", "成功下载的地址是:" + saveFile.getAbsolutePath());}});}
}
3.使用线程池下载
package com.example.downloading;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.view.View;import java.io.File;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;import cn.finalteam.okhttpfinal.FileDownloadCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.OkHttpFinal;
import cn.finalteam.okhttpfinal.OkHttpFinalConfiguration;public class MainActivity extends AppCompatActivity {private static ExecutorService executor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);OkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();OkHttpFinal.getInstance().init(builder.build());executor = Executors.newFixedThreadPool(5); // 创建一个拥有5个线程的线程池findViewById(R.id.Play_videos_and_pictures).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Showdownload("https:.........mp4","/storage/emulated/0/Download/2adfed77da173f.mp4");}});}public static void Download(final String url, final File saveFile) {Log.e("TAG","url===="+url);Log.e("TAG","saveFile===="+saveFile.getAbsolutePath());HttpRequest.download(url, saveFile, new FileDownloadCallback() {//开始下载@Overridepublic void onStart() {super.onStart();}//下载进度@Overridepublic void onProgress(int progress, long networkSpeed) {super.onProgress(progress, networkSpeed);Log.e("TAG", "进度值是:" + progress);}//下载失败@Overridepublic void onFailure() {super.onFailure();Log.e("TAG", "下载失败的地址是:" + url);}//下载完成(下载成功)@Overridepublic void onDone() {super.onDone();Log.e("TAG", "成功下载的地址是:" + saveFile.getAbsolutePath());}});}//设置多个下载private void Showdownload(String imageUrl, String imagepath) {//直接使用全局的线程池来执行下载任务,避免重复创建线程池for (int i1 = 0; i1 < 10; i1++) {executor.execute(new DownloadImageTask(imageUrl, imagepath));}}// 下载任务类class DownloadImageTask implements Runnable {private String imageUrl;private String imagepath;public DownloadImageTask(String imageUrl, String imagepath) {this.imageUrl = imageUrl;this.imagepath = imagepath;}@Overridepublic void run() {// 使用网络请求库下载文件Download(imageUrl, new File(imagepath));}}}