androidrestrx-javaretrofit2

Chunked streaming not behaving as expected Retrofit+RxJava


@GET("poll/session/{sessionId}/details")
Observable getSessionDetails(@Path("sessionId") String sessionId);

@GET("poll/session/{sessionId}/details")
@Streaming
Observable getSessionDetails(@Path("sessionId") String sessionId);

@Override
public Observable getSessionDetails(String sessionId) {
return sessionAPI.getSessionDetails(sessionId)
.flatMap(responseBody -> events(responseBody.source()));
}

public static Observable<String> events(BufferedSource source) {
    return Observable.create(subscriber -> {
        try {
            while (!source.exhausted()) {
                subscriber.onNext(source.readUtf8Line());
            }
        } catch (IOException e) {
            e.printStackTrace();
            subscriber.onError(e);
        }
        subscriber.onCompleted();
    });
}

The events() method does not get called unless all the chunks are done.

But the chunked streams are expected to be delivered chunk by chunk, which does not seem to be happening.

I have tried with and without the @Streaming annotation to the API, yet the behaviour is same.

I had used Android Retrofit 2 + RxJava: listen to endless stream as a reference to do my implementation


Solution

  • Alright guys I found the answer. It was because I was logging with the Body Attribute

    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    

    So, since the logger is waiting for the whole body to print it, it was behaving the way as mentioned in the problem.

    reference : Square's Retrofit response parsing logic: streaming?