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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//先初始化okhttp
OkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();
OkHttpFinal.getInstance().init(builder.build());
findViewById(R.id.Play_videos_and_pictures).setOnClickListener(new View.OnClickListener() {
@Override
public 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() {
//开始下载
@Override
public void onStart() {
super.onStart();
}
//下载进度
@Override
public void onProgress(int progress, long networkSpeed) {
super.onProgress(progress, networkSpeed);
Log.e("TAG", "进度值是:" + progress);
}
//下载失败
@Override
public void onFailure() {
super.onFailure();
Log.e("TAG", "下载失败的地址是:" + url);
}
//下载完成(下载成功)
@Override
public 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;
@Override
protected 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() {
@Override
public 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() {
//开始下载
@Override
public void onStart() {
super.onStart();
}
//下载进度
@Override
public void onProgress(int progress, long networkSpeed) {
super.onProgress(progress, networkSpeed);
Log.e("TAG", "进度值是:" + progress);
}
//下载失败
@Override
public void onFailure() {
super.onFailure();
Log.e("TAG", "下载失败的地址是:" + url);
}
//下载完成(下载成功)
@Override
public 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;
}
@Override
public void run() {
// 使用网络请求库下载文件
Download(imageUrl, new File(imagepath));
}
}
}