无法解析方法'subscribe(匿名org.reactivestreams.Subscriber <java.io.file>)

时间:2018-04-11 06:48:46

标签: java android rx-java2

我正在尝试使用Intent下载并发送pdf到PDF应用程序以显示文件,如此处answer of JDenais所示 这是下载pdf并通过Intent传递的代码。

public class PdfOpenHelper {

public static void openPdfFromUrl(final String pdfUrl, final Activity activity) {
    Observable.fromCallable(new Callable<File>() {
        @Override
        public File call() throws Exception {
            try {
                URL url = new URL(pdfUrl);
                URLConnection connection = url.openConnection();
                connection.connect();

                // download the file
                InputStream input = new BufferedInputStream(connection.getInputStream());
                File dir = new File(activity.getFilesDir(), "/shared_pdf");
                dir.mkdir();
                File file = new File(dir, "temp.pdf");
                OutputStream output = new FileOutputStream(file);

                byte data[] = new byte[1024];
                long total = 0;
                int count;
                while ((count = input.read(data)) != -1) {
                    total += count;
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
                return file;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<File>() {
                @Override
                public void onSubscribe(Subscription s) {

                }

                @Override
                public void onNext(File file) {
                    String authority = activity.getApplicationContext().getPackageName() + ".fileprovider";
                    Uri uriToFile = FileProvider.getUriForFile(activity, authority, file);

                    Intent shareIntent = new Intent(Intent.ACTION_VIEW);
                    shareIntent.setDataAndType(uriToFile, "application/pdf");
                    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    if (shareIntent.resolveActivity(activity.getPackageManager()) != null) {
                        activity.startActivity(shareIntent);
                    }
                }

                @Override
                public void onError(Throwable t) {

                }

                @Override
                public void onComplete() {

                }
            });
}
}

但我收到了错误

  

'无法解决subscribe(anonymous org.reactivestreams.Subscriber<java.io.File>)

上的方法.subscribe(new Subscriber<File>()

我是rx java的新手,我不知道代码有什么问题。

提前致谢

1 个答案:

答案 0 :(得分:2)

rx-java2消费者类型已更改。使用io.reactivex.Observer订阅io.reactivex.Observableorg.reactivestreams.Subscriber仅用于io.reactivex.Flowable订阅。

.subscribe(new Observer<File>() {
        @Override
        public void onSubscribe(Subscription s) {

        }

        @Override
        public void onNext(File file) { 

        }

        @Override
        public void onError(Throwable t) {

        }

        @Override
        public void onComplete() {

        }
    });