我通过改装下载时可以添加一个百分比吗?

时间:2016-11-03 13:46:56

标签: android retrofit2

我有这段代码:

   Retrofit retrofit = new Retrofit.Builder()
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .baseUrl(GithubService.SERVICE_ENDPOINT).build();

            GithubService service = retrofit.create(GithubService.class);

            service.getAirport()
                    .subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<List<Airport>>() {
                        List<Airport> airps = new ArrayList<Airport>();

                        @Override
                        public void onCompleted() {
                            for(final Airport air : airps) {
                                realm.executeTransactionAsync(new Realm.Transaction() {
                                    @Override
                                    public void execute(Realm mRealm) {
                                        AirportR airport = mRealm.createObject(AirportR.class);
                                        airport.setId(air.getId());
                                    }
                                }, new Realm.Transaction.OnSuccess() {
                                    @Override
                                    public void onSuccess() {
                                        Log.wtf("ok", "ok");
                                    }
                                }, new Realm.Transaction.OnError() {
                                    @Override
                                    public void onError(Throwable error) {
                                        Log.e("ok", "non vaaa");
                                    }
                                });
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            e.printStackTrace();
                        }

                        @Override
                        public void onNext(List<Airport> airports) {
                            airps = airports;
                        }
                    });
        }
    });

我需要下载百分比,因为机场太多......所以我想在textview中输入下载的百分比(我不知道如何在onNext()方法中做到这一点)以及在机场增加领域的百分比...... 有可能吗?

由于

1 个答案:

答案 0 :(得分:1)

我使用此函数显示下载进度,由rxjava和okhttp结合,希望它有所帮助。

private void initOkHttpClient() {
    mOkHttpClient = new OkHttpClient();
    mRequest = new Request.Builder().url(URL_JSON).build();
}

private void rxPublishProgress() {
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setMax(100);
    mProgressDialog.setCancelable(true);
    mProgressDialog.show();

    mRequest = new Request.Builder().url(URL_IMAGE).build();

    Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            try {
                InputStream inputStream;
                Response response = mOkHttpClient.newCall(mRequest).execute();
                if (response.isSuccessful()) {
                    inputStream = response.body().byteStream();
                    long len = response.body().contentLength();

                    String progress = "0";
                    subscriber.onNext(progress);

                    byte[] data = new byte[1024];
                    long total = 0;
                    int count;
                    while ((count = inputStream.read(data)) != -1) {
                        total += count;
                        progress = String.valueOf(total * 100 / len);
                        subscriber.onNext(progress);
                    }

                    inputStream.close();
                    subscriber.onCompleted();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).subscribeOn(Schedulers.newThread()).subscribe(new Subscriber<String>() {
        @Override
        public void onCompleted() {
            LogUtils.d("onCompleted");
            mProgressDialog.dismiss();
        }

        @Override
        public void onError(Throwable e) {
            LogUtils.d(e.toString());
            mProgressDialog.dismiss();
        }

        @Override
        public void onNext(final String progress) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mProgressDialog.setProgress(Integer.parseInt(progress));
                }
            });
        }
    });
}