0

I am trying to send an io.reactivex.Flowable from a Spring RestController to an Android application that uses Retrofit and Rxjava. If I use the browser to check what the Rest endpoint returns, I get a series of values as expected but in Android I get only one value and then it calls the onComplete method. What am I missing?

Spring Controller:

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

Retrofit repository:

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

Main service:

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();
                    }
                });
    }

Upon calling the useReactive() method, I get only one value "Flowable-..." and then "Completed".

Vlad
  • 59
  • 6

1 Answers1

1

Even though the Retrofit service has return type Flowable<String>, calling testReactive() will only make one HTTP call on the Android device.

The type Flowable is merely for compatibility, in practice it will end up being a Flowable that emits a single value and then terminates.

This is just how Retrofit works.

You would need to find another solution if you want to continually receive new values that are being emitted from the server, perhaps GRPC or polling the server.

David Rawson
  • 20,912
  • 7
  • 88
  • 124