Retrofit 2 and Rxjava for file downloading in Android

This post demonstrates how to download a zip file or any other file regardless of file size using retrofit 2 and rxjava in Android. Retrofit 2 for handling network task, and rxjava for handling the background task control flow. Using OkHttpClient as the Http client for Retrofit 2, and Okio for saving content to disk.

1. Make sure the following are included as dependencies in the gradle file.

compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'io.reactivex:rxjava:1.1.8'
compile 'io.reactivex:rxandroid:1.2.1'

2. The main method with the help of rxjava, composes every steps needed for downloading a file and save it to disk in Android. Replace and yourusername/awesomegames/archive/ with your actual url.

private void downloadZipFileRx() {
    RetrofitInterface downloadService = createService(RetrofitInterface.class, "");


3. All other methods that are used in the above method.

private Func1<Response<ResponseBody>, Observable<File>> processResponse() {
    return new Func1<Response<ResponseBody>, Observable<File>>() {
        public Observable<File> call(Response<ResponseBody> responseBodyResponse) {
            return saveToDiskRx(responseBodyResponse);

private Observable<File> saveToDiskRx(final Response<ResponseBody> response) {
    return Observable.create(new Observable.OnSubscribe<File>() {
        public void call(Subscriber<? super File> subscriber) {
            try {
                String header = response.headers().get("Content-Disposition");
                String filename = header.replace("attachment; filename=", "");

                new File("/data/data/" + getPackageName() + "/games").mkdir();
                File destinationFile = new File("/data/data/" + getPackageName() + "/games/" + filename);

                BufferedSink bufferedSink = Okio.buffer(Okio.sink(destinationFile));

            } catch (IOException e) {

private Observer<File> handleResult() {
    return new Observer<File>() {
        public void onCompleted() {
            Log.d(TAG, "onCompleted");

        public void onError(Throwable e) {
            Log.d(TAG, "Error " + e.getMessage());

        public void onNext(File file) {
            Log.d(TAG, "File downloaded to " + file.getAbsolutePath());

public <T> T createService(Class<T> serviceClass, String baseUrl) {
    Retrofit retrofit = new Retrofit.Builder()
            .client(new OkHttpClient.Builder().build())
    return retrofit.create(serviceClass);

Search within Codexpedia

Custom Search

Search the entire web

Custom Search