Android Retrofit + Rxjava flowable太早完成

问题描述 投票:0回答:1

我正在尝试从Spring RestController向使用Retrofit和Rxjava的Android应用程序发送io.reactivex.Flowable。如果使用浏览器检查Rest终结点返回的值,则会得到一系列预期值,但在Android中,我只会得到一个值,然后调用onComplete方法。我想念什么?

Spring控制器:

@GetMapping("/api/reactive")
    public Flowable<String> reactive() {
        return Flowable.interval(1, TimeUnit.SECONDS).map(sequence -> "\"Flowable-" + LocalTime.now().toString() + "\"");
    }

翻新储存库:

@GET("reactive")
    Flowable<String> testReactive();

主要服务:

public useReactive() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Values.BASE_URL)
                .addConverterFactory(JacksonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        userRepository = retrofit.create(UserRepository.class);

        Flowable<String> reactive = userRepository.testReactive();
        Disposable disp = reactive.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new ResourceSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        logger.log(Level.INFO, s);
                        Toast.makeText(authActivity, s, Toast.LENGTH_SHORT).show();
                    }

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

                    @Override
                    public void onComplete() {
                        logger.log(Level.INFO, "Completed");
                        Toast.makeText(authActivity, "Completed", Toast.LENGTH_SHORT).show();
                    }
                });
    }

在调用useReactive()方法时,我只得到一个值“ Flowable -...”,然后得到“ Completed”。

android spring-boot retrofit2 rx-java2 flowable
1个回答
1
投票

即使翻新服务的返回类型为Flowable<String>,调用testReactive()只会在Android设备上进行一次HTTP调用。

类型Flowable仅出于兼容性考虑,实际上它最终将是一个发出单个值然后终止的Flowable

这就是改造的工作方式。

[如果要持续接收从服务器发出的新值,可能是GRPC或轮询服务器,则需要找到其他解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.