rx-java

Android: Polling a server with Retrofit

痞子三分冷 提交于 2019-12-31 16:35:29
问题 I'm building a 2 Player game on Android. The game works turnwise, so player 1 waits until player 2 made his input and vice versa. I have a webserver where I run an API with the Slim Framework. On the clients I use Retrofit. So on the clients I would like to poll my webserver (I know it's not the best approach) every X seconds to check whether there was an input from player 2 or not, if yes change UI (the gameboard). Dealing with Retrofit I came across RxJava. My problem is to figure out

How do I get Response body when there is an error when using Retrofit 2.0 Observables

吃可爱长大的小学妹 提交于 2019-12-31 08:55:34
问题 I am using Retrofit 2.0 to make api calls that return Observables. It all works good when the call went through fine and the response is as expected. Now let's say we have an error response, it throws an onError. I would like to read the response body even when it is an Error. Example @FormUrlEncoded @POST("tokenLogin") Observable<LoginResponse> loginWithToken( @Field("token") String pin ); When the request and response are valid, I get the right observable and onError is being called as

Call another Retrofit call on Subject emission

折月煮酒 提交于 2019-12-31 05:19:25
问题 I have a following class: public class SessionStore { Subject<Session, Session> subject; public SessionStore() { subject = new SerializedSubject<>(BehaviorSubject.create(new Session()); } public void set(Session session) { subject.onNext(session); } public Observable<UserSession> observe() { return subject.distinctUntilChanged(); } } In activity I observe the session and perform network operation on each change: private Subscription init() { return sessionStore .observe() .flatMap(new Func1

RxJava 2 / Retrofit 2 - NetworkOnMainThreadException

空扰寡人 提交于 2019-12-30 06:43:06
问题 I need to perform a request, if my token is expired I need to refresh it and retry the request. This is how I'm trying to do it, at the moment I can refresh the token but it throws me a NetworkOnMainThreadException. It finishes the request,update the token and reaches logs, but that exception its killing me. How can I avoid that? public Observable<Estabelecimento> listarEstabelecimentos() { return Observable.defer(this::getListarEstabelecimentoObservable) .retryWhen(throwableObservable ->

Kotlin and RxJava - Why is my Single.zip() not compiling?

流过昼夜 提交于 2019-12-30 02:36:07
问题 I'm going a little crazy here. I'm trying to create an Observable<BigDecimal> extension function (against RxJava 2.x) to emit the average of the emissions, but I'm getting a compile error with the Single.zip() function. Does anybody have any ideas what I'm doing wrong? I tried to be explicit with all my types too and that didn't work... import io.reactivex.Observable import io.reactivex.Single import java.math.BigDecimal fun Observable<BigDecimal>.sum() = reduce { total, next -> total + next

RxJava and Cached Data

大城市里の小女人 提交于 2019-12-29 10:38:02
问题 I'm still fairly new to RxJava and I'm using it in an Android application. I've read a metric ton on the subject but still feel like I'm missing something. I have the following scenario: I have data stored in the system which is accessed via various service connections (AIDL) and I need to retrieve data from this system (1-n number of async calls can happen). Rx has helped me a ton in simplifying this code. However, this entire process tends to take a few seconds (upwards of 5 seconds+)

RxJava and Cached Data

梦想的初衷 提交于 2019-12-29 10:37:40
问题 I'm still fairly new to RxJava and I'm using it in an Android application. I've read a metric ton on the subject but still feel like I'm missing something. I have the following scenario: I have data stored in the system which is accessed via various service connections (AIDL) and I need to retrieve data from this system (1-n number of async calls can happen). Rx has helped me a ton in simplifying this code. However, this entire process tends to take a few seconds (upwards of 5 seconds+)

What is the difference between an Observer and a Subscriber?

百般思念 提交于 2019-12-29 10:32:31
问题 I am trying to decipher the following function: Subscription getCar(id, Observer<Car> observer) { return getCarDetails(id, new Observer<CarDetails> { @Override onNext(CarDetails details) { observer.onNext(details.getCar()); } }); } I got a good intro to rxjava from http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/ but it only mentioned Observer in passing, saying you'll be using Subscriber most of the time to consumer items emitted from an Observable. Can someone explain to me What is

rxjava merge observables of different type

给你一囗甜甜゛ 提交于 2019-12-29 04:27:08
问题 I'm new to rxjava. I need to combine two observables that emit objects of different type. Something like Observable<Milk> and Observable<Cereals> and get a Observable<CerealsWithMilk> . I couldn't find any operator for something like this. What would be the rx way of doing something like this? Note that Milk and Cereals are async. 回答1: It's hard to say without knowing exactly what you need, but possibly zip() or combineLatest(). zip will take both Observable<Milk> and Observable<Cereals> and

How to handle exceptions thrown by observer's onNext in RxJava?

爱⌒轻易说出口 提交于 2019-12-28 11:59:18
问题 Consider the following example: Observable.range(1, 10).subscribe(i -> { System.out.println(i); if (i == 5) { throw new RuntimeException("oops!"); } }, Throwable::printStackTrace); This outputs numbers from 1 to 5 and then prints the exception. What I want to achieve is make the observer stay subscribed and continue to run after throwing an exception, i.e. print all numbers from 1 to 10. I have tried using retry() and other various error handling operators, but, as said in the documentation,