Retrofit多线程下载

来源:互联网 发布:16总决赛骑士数据 编辑:程序博客网 时间:2024/06/08 11:54
retrofit接口类public interface GetInterface {//    @GET("iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=vedio")//    Observable<Bean> getData_Banner();    @Streaming/*大文件需要加入这个判断,防止下载过程中写入到内存中*/    @GET    Observable<ResponseBody> download(@Header("RANGE") String start, @Url String url);}封装的Retrofit
public class BaseService {    public static final String API_BASE_URL = "https://api.github.com";    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();    private static Retrofit.Builder builder =            new Retrofit.Builder()                    .baseUrl("http://result.eolinker.com/")                    .addConverterFactory(GsonConverterFactory.create())                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create());    public static <S> S createService(Class<S> serviceClass) {        Retrofit retrofit = builder.client(httpClient.build()).build();        return retrofit.create(serviceClass);    }}调用
public class IdownModel implements IDwonFile {    String url3="";    @Override    public void getData(String start, String url, final String url2, final GetCallback callback) {        this.url3 = url;        BaseService.createService(GetInterface.class).download(start, url3)                .subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new Observer<ResponseBody>() {                    @Override                    public void onSubscribe(Disposable d) {                    }                    @Override                    public void onNext(ResponseBody responseBody) {                        final long l = responseBody.contentLength();                        Log.i("=====length=====", "onNext: " + l);                        downloadTask(l, url2);                    }                    @Override                    public void onError(Throwable e) {                    }                    @Override                    public void onComplete() {                    }                });    }    public void downloadTask(long length, String url2) {        final File file = new File(url2);        if (!file.exists()) {            try {                file.createNewFile();            } catch (IOException e) {                e.printStackTrace();            }        }        //例如  1000kb  3   333        int threadNum = 5;        long blockSize = length / threadNum;        //计算出下载块以后   创建线程执行下载操作        for (int i = 0; i < threadNum; i++) {            //计算开始位置            final long startPosition = blockSize * i;            //让最后一个线程下载的大小是正好的,  总长度 - 除了最后一个块的大小和            if (i == threadNum - 1) {                blockSize = length - blockSize * (threadNum - 1);            }            //"Range:bytes=0-100"            String range = "bytes=" + startPosition + "-" + (startPosition + blockSize - 1);            //对于retrofit的网络请求  需要  请求地址 需要设置请求头Range            Log.i("====url====", "downloadTask: "+url3);            Log.i("====ramge====", "downloadTask: "+range);            BaseService.createService(GetInterface.class).download(range, url3)                    .subscribeOn(Schedulers.io())                    .observeOn(Schedulers.io())                    .subscribe(new Observer<ResponseBody>() {                        @Override                        public void onSubscribe(Disposable d) {                        }                        @Override                        public void onNext(ResponseBody responseBody) {                            try {                                BufferedInputStream bis = new BufferedInputStream(responseBody.byteStream());                                RandomAccessFile raf = new RandomAccessFile(file, "rwd");                                raf.seek(startPosition);                                byte[] buff = new byte[1024 * 8];                                int len = 0;                                while ((len = bis.read(buff)) != -1) {                                    raf.write(buff, 0, len);                                }                            } catch (IOException e) {                                e.printStackTrace();                            }                        }                        @Override                        public void onError(Throwable e) {                        }                        @Override                        public void onComplete() {                        }                    });        }    }}依赖 
//    Retrofit支持gson解析Gsonconverterfactory    compile 'com.squareup.retrofit2:converter-gson:2.1.0'//    Retrofit    compile 'com.squareup.retrofit2:retrofit:2.0.2'//    Okhttp    compile 'com.squareup.okhttp3:okhttp:3.1.2'//    Rxjava2    compile 'io.reactivex.rxjava2:rxjava:2.1.1'    compile "io.reactivex.rxjava2:rxandroid:2.0.1"//adapter    compile 'com.squareup.retrofit2:adapter-rxjava2:2.2.0'//    Retrofit支持scalars    compile 'com.squareup.retrofit2:converter-scalars:2.0.0'




原创粉丝点击