Android RxJava2+Retrofit2單檔案下載監聽進度封裝

來源:互聯網
上載者:User

標籤:分享圖片   default   roi   步驟   creat   ons   abstract   over   分享   

RxJava2和Retrofit2用的越來越多,最近也在封裝一個通用的網路請求庫,其中就包括了單檔案下載的方法,所以這裡進行記錄。文末附帶Demo

由於網上很多的方法都是使用攔截器進行進度的監聽,個人覺得使用起來非常複雜和,所以取了個巧,在檔案寫入到硬碟的時候對檔案讀寫進行監聽,就解決了retrofit2下載檔案沒有進度監聽的問題。

先上封裝之後的使用代碼,使用簡單,直接回調下載之後的檔案

封裝步驟
  • 1.定義介面(使用的時候傳入完整的url,@Streaming註解可用於下載大檔案)
@Streaming    @GET    Observable<ResponseBody> downLoadFile(@NonNull @Url String url);
  • 2.檔案下載的回調方法和檔案儲存方法
public abstract class FileDownLoadObserver<T> extends DefaultObserver<T> {    @Override    public void onNext(T t) {        onDownLoadSuccess(t);    }    @Override    public void onError(Throwable e) {        onDownLoadFail(e);    }    //可以重寫,具體可由子類實現    @Override    public void onComplete() {    }    //下載成功的回調    public abstract void onDownLoadSuccess(T t);    //下載失敗回調    public abstract void onDownLoadFail(Throwable throwable);    //下載進度監聽    public abstract void onProgress(int progress,long total);    /**     * 將檔案寫入本地     * @param responseBody 請求結果全體     * @param destFileDir 目標檔案夾     * @param destFileName 目標檔案名     * @return 寫入完成的檔案     * @throws IOException IO異常     */    public File saveFile(ResponseBody responseBody, String destFileDir, String destFileName) throws IOException {        InputStream is = null;        byte[] buf = new byte[2048];        int len = 0;        FileOutputStream fos = null;        try {            is = responseBody.byteStream();            final long total = responseBody.contentLength();            long sum = 0;            File dir = new File(destFileDir);            if (!dir.exists()) {                dir.mkdirs();            }            File file = new File(dir, destFileName);            fos = new FileOutputStream(file);            while ((len = is.read(buf)) != -1) {                sum += len;                fos.write(buf, 0, len);                final long finalSum = sum;                //這裡就是對進度的監聽回調                onProgress((int) (finalSum * 100 / total),total);            }            fos.flush();            return file;        } finally {            try {                if (is != null) is.close();            } catch (IOException e) {                e.printStackTrace();            }            try {                if (fos != null) fos.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}
  • 3.Retorfit構建
/**     * 下載單檔案,該方法不支援斷點下載     *     * @param url                  檔案地址     * @param destDir              隱藏檔夾     * @param fileName             隱藏檔名     * @param fileDownLoadObserver 監聽回調     */    public void downloadFile(@NonNull String url, final String destDir, final String fileName, final FileDownLoadObserver<File> fileDownLoadObserver) {        Retrofit retrofit = new Retrofit.Builder()                .client(new OkHttpClient())                .baseUrl(BASE_API.BASE_URL)                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                .addConverterFactory(GsonConverterFactory.create())                .build();        retrofit                .create(BASE_API.class)                .downLoadFile(url)                .subscribeOn(Schedulers.io())//subscribeOn和ObserOn必須在io線程,如果在主線程會出錯                .observeOn(Schedulers.io())                .observeOn(Schedulers.computation())//需要                .map(new Function<ResponseBody, File>() {                    @Override                    public File apply(@NonNull ResponseBody responseBody) throws Exception {                        return fileDownLoadObserver.saveFile(responseBody, destDir, fileName);                    }                })                .observeOn(AndroidSchedulers.mainThread())                .subscribe(fileDownLoadObserver);    }
  • 4使用,如一開始的圖
downloadFile("檔案下載url","目標儲存路徑","檔案名稱",new FileDownLoadObserver<File>() {                            @Override                            public void onDownLoadSuccess(File file) {                            }                            @Override                            public void onDownLoadFail(Throwable throwable) {                            }                            @Override                            public void onProgress(int progress,long total) {                            }                        });
  • 項目Demo地址

Android RxJava2+Retrofit2單檔案下載監聽進度封裝

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.