当前位置:   article > 正文

安卓使用okhttpfinal下载文件,附带线程池下载使用

安卓使用okhttpfinal下载文件,附带线程池下载使用

1.导入okhttp

implementation 'cn.finalteam:okhttpfinal:2.0.7'

2.单个下载

  1. package com.example.downloading;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import java.io.File;
  7. import cn.finalteam.okhttpfinal.FileDownloadCallback;
  8. import cn.finalteam.okhttpfinal.HttpRequest;
  9. import cn.finalteam.okhttpfinal.OkHttpFinal;
  10. import cn.finalteam.okhttpfinal.OkHttpFinalConfiguration;
  11. public class MainActivity extends AppCompatActivity {
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);
  16. //先初始化okhttp
  17. OkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();
  18. OkHttpFinal.getInstance().init(builder.build());
  19. findViewById(R.id.Play_videos_and_pictures).setOnClickListener(new View.OnClickListener() {
  20. @Override
  21. public void onClick(View view) {
  22. Download("https:...../2_0ee2238f8f8cd7b3773cc6d82dafb756.mp4",
  23. new File("/storage/emulated/0/Download/2adfed77da173f.mp4"));
  24. }
  25. });
  26. }
  27. public static void Download(final String url, final File saveFile) {
  28. Log.e("TAG","url===="+url);
  29. Log.e("TAG","saveFile===="+saveFile.getAbsolutePath());
  30. HttpRequest.download(url, saveFile, new FileDownloadCallback() {
  31. //开始下载
  32. @Override
  33. public void onStart() {
  34. super.onStart();
  35. }
  36. //下载进度
  37. @Override
  38. public void onProgress(int progress, long networkSpeed) {
  39. super.onProgress(progress, networkSpeed);
  40. Log.e("TAG", "进度值是:" + progress);
  41. }
  42. //下载失败
  43. @Override
  44. public void onFailure() {
  45. super.onFailure();
  46. Log.e("TAG", "下载失败的地址是:" + url);
  47. }
  48. //下载完成(下载成功)
  49. @Override
  50. public void onDone() {
  51. super.onDone();
  52. Log.e("TAG", "成功下载的地址是:" + saveFile.getAbsolutePath());
  53. }
  54. });
  55. }
  56. }

3.使用线程池下载 

  1. package com.example.downloading;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import java.io.File;
  7. import java.util.List;
  8. import java.util.concurrent.ExecutorService;
  9. import java.util.concurrent.Executors;
  10. import cn.finalteam.okhttpfinal.FileDownloadCallback;
  11. import cn.finalteam.okhttpfinal.HttpRequest;
  12. import cn.finalteam.okhttpfinal.OkHttpFinal;
  13. import cn.finalteam.okhttpfinal.OkHttpFinalConfiguration;
  14. public class MainActivity extends AppCompatActivity {
  15. private static ExecutorService executor;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);
  20. OkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();
  21. OkHttpFinal.getInstance().init(builder.build());
  22. executor = Executors.newFixedThreadPool(5); // 创建一个拥有5个线程的线程池
  23. findViewById(R.id.Play_videos_and_pictures).setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View view) {
  26. Showdownload("https:.........mp4",
  27. "/storage/emulated/0/Download/2adfed77da173f.mp4");
  28. }
  29. });
  30. }
  31. public static void Download(final String url, final File saveFile) {
  32. Log.e("TAG","url===="+url);
  33. Log.e("TAG","saveFile===="+saveFile.getAbsolutePath());
  34. HttpRequest.download(url, saveFile, new FileDownloadCallback() {
  35. //开始下载
  36. @Override
  37. public void onStart() {
  38. super.onStart();
  39. }
  40. //下载进度
  41. @Override
  42. public void onProgress(int progress, long networkSpeed) {
  43. super.onProgress(progress, networkSpeed);
  44. Log.e("TAG", "进度值是:" + progress);
  45. }
  46. //下载失败
  47. @Override
  48. public void onFailure() {
  49. super.onFailure();
  50. Log.e("TAG", "下载失败的地址是:" + url);
  51. }
  52. //下载完成(下载成功)
  53. @Override
  54. public void onDone() {
  55. super.onDone();
  56. Log.e("TAG", "成功下载的地址是:" + saveFile.getAbsolutePath());
  57. }
  58. });
  59. }
  60. //设置多个下载
  61. private void Showdownload(String imageUrl, String imagepath) {
  62. //直接使用全局的线程池来执行下载任务,避免重复创建线程池
  63. for (int i1 = 0; i1 < 10; i1++) {
  64. executor.execute(new DownloadImageTask(imageUrl, imagepath));
  65. }
  66. }
  67. // 下载任务类
  68. class DownloadImageTask implements Runnable {
  69. private String imageUrl;
  70. private String imagepath;
  71. public DownloadImageTask(String imageUrl, String imagepath) {
  72. this.imageUrl = imageUrl;
  73. this.imagepath = imagepath;
  74. }
  75. @Override
  76. public void run() {
  77. // 使用网络请求库下载文件
  78. Download(imageUrl, new File(imagepath));
  79. }
  80. }
  81. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/182021
推荐阅读
相关标签
  

闽ICP备14008679号