Android Retrofit+Okhttp断点续传


1.  断点续传原理

在本地下载过程中要使用数据库实时存储到底存储到文件的哪个位置了,这样点击开始继续传递时,才能通过HTTP的GET请求中的setRequestProperty()方法可以告诉服务器,数据从哪里开始,到哪里结束。同时在本地的文件写入时,RandomAccessFileseek()方法也支持在文件中的任意位置进行写入操作。同时通过广播将子线程的进度告诉Activity的ProcessBar。

 

2.  Activity的按钮响应

当点击开始按钮时,将url写在了FileInfo类的对象info中并通过Intent从Activity传递到了Service中。这里使用setAction()来区分是开始按钮还是暂停按钮。


首先是相关的权限 在清单文件里面 添加

[html]  view plain  copy
  1. <uses-permission android:name="android.permission.INTERNET"/>  
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
  3. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  4. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  
  5. <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />  
第二就是在App的build里面添加相关的依赖
[html]  view plain  copy
  1. compile 'io.reactivex:rxjava:1.0.14'  
  2. compile 'io.reactivex:rxandroid:1.0.1'  
  3.   
  4. compile 'com.squareup.retrofit2:retrofit:2.1.0'  
  5. compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'  
  6. compile 'io.reactivex:rxandroid:1.1.0'  
  7. compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'  
  8. compile 'com.squareup.retrofit2:converter-gson:2.1.0'  
  9.   
  10. compile 'com.squareup.okio:okio:1.5.0'  
  11. compile 'com.squareup.okhttp3:okhttp:3.2.0'  
  12. compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'  
  13.   
  14. compile 'com.jakewharton:butterknife:7.0.1'  
接下来就是相关的布局 一个 文字 和相关的三个button 按钮 开始 暂停 继续 来实现 相关布局和简单的效果

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_main"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     android:orientation="vertical"  
  12.     tools:context="com.eightgroup.jinchengxiancheng.MainActivity">  
  13.     <ProgressBar  
  14.         android:id="@+id/progressBar"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_marginLeft="10dp"  
  18.         android:layout_marginRight="10dp"  
  19.         android:max="100"  
  20.         style="?android:attr/progressBarStyleHorizontal" />  
  21.     <Button  
  22.         android:id="@+id/downloadButton"  
  23.         android:onClick="downloadButtons"  
  24.         android:layout_marginTop="10dp"  
  25.         android:layout_marginLeft="10dp"  
  26.         android:layout_marginRight="10dp"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="下载"/>  
  30.     <Button  
  31.         android:onClick="cancel_buttons"  
  32.         android:id="@+id/cancel_button"  
  33.         android:layout_marginTop="10dp"  
  34.         android:layout_marginLeft="10dp"  
  35.         android:layout_marginRight="10dp"  
  36.         android:layout_width="match_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:text="暂停"/>  
  39.     <Button  
  40.         android:onClick="continue_buttons"  
  41.         android:id="@+id/continue_button"  
  42.         android:layout_marginTop="10dp"  
  43.         android:layout_marginLeft="10dp"  
  44.         android:layout_marginRight="10dp"  
  45.         android:layout_width="match_parent"  
  46.         android:layout_height="wrap_content"  
  47.         android:text="继续"/>  
  48. </LinearLayout>  

//然后创建两个相关的类 来实现

第一个

ProgressDownloader
 //在下载、暂停后的继续下载中可复用同一个client对象

//每次下载需要新建新的Call对象
[html]  view plain  copy
  1. public class ProgressDownloader {  
  2.     public static final String TAG = "ProgressDownloader";  
  3.     private ProgressResponseBody.ProgressListener progressListener;  
  4.     private String url;  
  5.     private OkHttpClient client;  
  6.     private File destination;  
  7.     private Call call;  
  8.     public ProgressDownloader(String url, File destination, ProgressResponseBody.ProgressListener progressListener) {  
  9.         this.url = url;  
  10.         this.destination = destination;  
  11.         this.progressListener = progressListener;  
  12.         //在下载、暂停后的继续下载中可复用同一个client对象  
  13.         client = getProgressClient();  
  14.     }  
  15.     //每次下载需要新建新的Call对象  
  16.     private Call newCall(long startPoints) {  
  17.         Request request = new Request.Builder()  
  18.                 .url(url)  
  19.                 .header("RANGE", "bytes=" + startPoints + "-")//断点续传要用到的,指示下载的区间  
  20.                 .build();  
  21.         return client.newCall(request);  
  22.     }  
  23.     public OkHttpClient getProgressClient() {  
  24.         // 拦截器,用上ProgressResponseBody  
  25.         Interceptor interceptor = new Interceptor() {  
  26.             @Override  
  27.             public Response intercept(Chain chain) throws IOException {  
  28.                 Response originalResponse = chain.proceed(chain.request());  
  29.                 return originalResponse.newBuilder()  
  30.                         .body(new ProgressResponseBody(originalResponse.body(), progressListener))  
  31.                         .build();  
  32.             }  
  33.         };  
  34.         return new OkHttpClient.Builder()  
  35.                 .addNetworkInterceptor(interceptor)  
  36.                 .build();  
  37.     }  
  38.     // startsPoint指定开始下载的点  
  39.     public void download(final long startsPoint) {  
  40.         call = newCall(startsPoint);  
  41.         call.enqueue(new Callback() {  
  42.             @Override  
  43.             public void onFailure(Call call, IOException e) {  
  44.             }  
  45.             @Override  
  46.             public void onResponse(Call call, Response response) throws IOException {  
  47.                 save(response, startsPoint);  
  48.             }  
  49.         });  
  50.     }  
  51.     public void pause() {  
  52.         if(call!=null){  
  53.             call.cancel();  
  54.         }  
  55.     }  
  56.     private void save(Response response, long startsPoint) {  
  57.         ResponseBody body = response.body();  
  58.         InputStream in = body.byteStream();  
  59.         FileChannel channelOut = null;  
  60.         // 随机访问文件,可以指定断点续传的起始位置  
  61.         RandomAccessFile randomAccessFile = null;  
  62.         try {  
  63.             randomAccessFile = new RandomAccessFile(destination, "rwd");  
  64.             //Chanel NIO中的用法,由于RandomAccessFile没有使用缓存策略,直接使用会使得下载速度变慢,亲测缓存下载3.3秒的文件,用普通的RandomAccessFile需要20多秒。  
  65.             channelOut = randomAccessFile.getChannel();  
  66.             // 内存映射,直接使用RandomAccessFile,是用其seek方法指定下载的起始位置,使用缓存下载,在这里指定下载位置。  
  67.             MappedByteBuffer mappedBuffer = channelOut.map(FileChannel.MapMode.READ_WRITE, startsPoint, body.contentLength());  
  68.             byte[] buffer = new byte[1024];  
  69.             int len;  
  70.             while ((len = in.read(buffer)) != -1) {  
  71.                 mappedBuffer.put(buffer, 0, len);  
  72.             }  
  73.         } catch (IOException e) {  
  74.             e.printStackTrace();  
  75.         }finally {  
  76.             try {  
  77.                 in.close();  
  78.                 if (channelOut != null) {  
  79.                     channelOut.close();  
  80.                 }  
  81.                 if (randomAccessFile != null) {  
  82.                     randomAccessFile.close();  
  83.                 }  
  84.             } catch (IOException e) {  
  85.                 e.printStackTrace();  
  86.             }  
  87.         }  
  88.     }  
  89. }  

接下来是第二个类 需要继承 ResponBoay
[html]  view plain  copy
  1. public class ProgressResponseBody extends ResponseBody {  
  2.   
  3.     //设置对外访问的进度监听  
  4.     public interface ProgressListener {  
  5.         void onPreExecute(long contentLength);  
  6.   
  7.         void update(long totalBytes, boolean done);  
  8.     }  
  9.   
  10.     private final ResponseBody responseBody;  
  11.     private final ProgressListener progressListener;  
  12.     private BufferedSource bufferedSource;  
  13.   
  14.     public ProgressResponseBody(ResponseBody responseBody, ProgressListener progressListener) {  
  15.         this.responseBody = responseBody;  
  16.         this.progressListener = progressListener;  
  17.         if (progressListener != null) {  
  18.             progressListener.onPreExecute(contentLength());  
  19.         }  
  20.     }  
  21.   
  22.     @Override  
  23.     public MediaType contentType() {  
  24.         return responseBody.contentType();  
  25.     }  
  26.   
  27.     @Override  
  28.     public long contentLength() {  
  29.         return responseBody.contentLength();  
  30.     }  
  31.   
  32.     @Override  
  33.     public BufferedSource source() {  
  34.         if (bufferedSource == null) {  
  35.             bufferedSource = Okio.buffer(source(responseBody.source()));  
  36.         }  
  37.         return bufferedSource;  
  38.     }  
  39.   
  40.     private Source source(Source source) {  
  41.         return new ForwardingSource(source) {  
  42.             long totalBytes = 0L;  
  43.   
  44.             @Override  
  45.             public long read(Buffer sink, long byteCount) throws IOException {  
  46.                 long bytesRead = super.read(sink, byteCount);  
  47.                 // read() returns the number of bytes read, or -1 if this source is exhausted.  
  48.                 totalBytes += bytesRead != -1 ? bytesRead : 0;  
  49.                 if (null != progressListener) {  
  50.                     progressListener.update(totalBytes, bytesRead == -1);  
  51.                 }  
  52.                 return bytesRead;  
  53.             }  
  54.         };  
  55.     }  
  56. }  
下面就是主要Activity的 需要 实现接口 对应的三个线程

[html]  view plain  copy
  1. public class MainActivity extends AppCompatActivity implements ProgressResponseBody.ProgressListener {  
  2.     public static final String TAG = "MainActivity";  
  3.     public static final String PACKAGE_URL = "http://gdown.baidu.com/data/wisegame/df65a597122796a4/weixin_821.apk";  
  4.   
  5.     private ProgressBar progressBar;  
  6.     private long breakPoints;  
  7.     private ProgressDownloader downloader;  
  8.     private File file;  
  9.     private long totalBytes;  
  10.     private long contentLength;  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         progressBar = (ProgressBar) findViewById(R.id.progressBar);  
  16.     }  
  17.   
  18.     public void downloadButtons(View view){  
  19.   
  20.         // 新下载前清空断点信息  
  21.         breakPoints = 0L;  
  22.         // 下载的位置  
  23.         file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "sample.apk");  
  24.         downloader = new ProgressDownloader(PACKAGE_URL, file, this);  
  25.         downloader.download(0L);  
  26.     }  
  27.     public void cancel_buttons(View view){  
  28.         downloader.pause();  
  29.         Toast.makeText(this, "下载暂停", Toast.LENGTH_SHORT).show();  
  30.         // 存储此时的totalBytes,即断点位置。  
  31.         breakPoints = totalBytes;  
  32.     }  
  33.     public void continue_buttons(View view){  
  34.         downloader.download(breakPoints);  
  35.     }  
  36.   
  37.     @Override  
  38.     public void onPreExecute(long contentLength) {  
  39.         // 文件总长只需记录一次,要注意断点续传后的contentLength只是剩余部分的长度  
  40.         if (this.contentLength == 0L) {  
  41.             this.contentLength = contentLength;  
  42.             progressBar.setMax((int) (contentLength / 1024));  
  43.         }  
  44.     }  
  45.   
  46.     @Override  
  47.     public void update(long totalBytes, boolean done) {  
  48.         // 注意加上断点的长度  
  49.         this.totalBytes = totalBytes + breakPoints;  
  50.         progressBar.setProgress((int) (totalBytes + breakPoints) / 1024);  
  51.         if (done) {  
  52.             // 切换到主线程  
  53.             Observable  
  54.                     .empty()  
  55.                     .observeOn(AndroidSchedulers.mainThread())  
  56.                     .doOnCompleted(new Action0() {  
  57.                         @Override  
  58.                         public void call() {  
  59.                             Toast.makeText(MainActivity.this, "下载完成", Toast.LENGTH_SHORT).show();  
  60.                         }  
  61.                     })  
  62.                     .subscribe();  
  63.         }  
  64.     }  
  65. }  

猜你喜欢

转载自blog.csdn.net/taa1007/article/details/78502843