rx-android

Using Retrofit and RxJava, how do I deserialize JSON when it doesn't map directly to a model object?

怎甘沉沦 提交于 2020-01-04 05:41:16
问题 The API I'm working with returns objects (and their containing objects) in a "flat" format and I'm having trouble getting this to work elegantly with Retrofit and RxJava. Consider this JSON response for an /employees/{id} endpoint: { "id": "123", "id_to_name": { "123" : "John Doe" }, "id_to_age": { "123" : 30 } } Using Retrofit and RxJava, how do I deserialize this to a Employee object with fields for name and age ? Ideally I'd like RxJava's onNext method to be called with an Employee object.

rxJava, Refresh api data periodically

允我心安 提交于 2020-01-04 02:30:46
问题 I am using the following observable to call retrofit api then save the response into cache file: @Override public Observable<StoryCollectionEntity> storyEntityList(final int page) { return this.restApi.storyCollection(id, page) .doOnNext(saveStoryCollectionToCacheAction) .onErrorResumeNext(CloudNewsDataStore.this.mNewsCache.getStories(page)); } This works as expected. my question is: how can i make this observer returns api response periodically? let's say, user wants to refresh the data

Combine Observable with second Observable which uses results from from the first one

Deadly 提交于 2020-01-02 05:23:21
问题 I have two methods returning Observable: Observable<String> firstObservable(); Observable<String> secondObservable(String value); For each result from the first Observable I get new instance of the second Observable. For each result from the second observable I would return object with combined results. firstObservable -> x----x----x----x----x \ \ \ \ \ secondObservable -> y(x)-y(x)-y(x)-y(x)-y(x) \ \ \ \ \ result -> {x,y}-{x,y}-{x,y}-{x,y}-{x,y} How can this be done? 回答1: There's an

Downloading files using OkHttp, Okio and RxJava

牧云@^-^@ 提交于 2020-01-01 19:48:21
问题 I'm trying to download files using OkHttp and writing to disk with Okio. Also I've created an rx observable for this process. It is working, however it is noticeably slower than what I had previously used (Koush's Ion library). Here's how I create the observable: public Observable<FilesWrapper> download(List<Thing> things) { return Observable.from(things) .map(thing -> { File file = new File(getExternalCacheDir() + File.separator + thing.getName()); if (!file.exists()) { Request request = new

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

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

RxAndroidBle keeping a persistant connection + Write/Notification handling

我们两清 提交于 2019-12-28 06:33:48
问题 I am building an Android application that has specific requirements regarding Bluetooth Low Energy. I need to write to a Write-only characteristic and receive responses on a separate notification characteristic, and I need to do it in many, many activities. Is there a Rx way to send a request on the 1st characteristic, wait for the answer on the second one, then proceed to another request? Also, to share my instance of RxAndroidBle I thought about doing some sort of BleManager Singleton where

Android: Unable to handle UnknownHostException [with Retrofit 2, okhttp3, rxandroid 2]

大憨熊 提交于 2019-12-25 17:01:59
问题 I've got an Android app which has been in production for some time. I've received a fatal report to Firebase (log is below) but it's not clear to me how to avoid it next time. I've found similar stack trace here java.net.UnknownHostException Unable to resolve host "accounts.google.com": No address associated with hostname while inserting rows in bigquery but they were able resolved it with: <uses-permission android:name="android.permission.INTERNET" /> But that's not my case. For network

Android: Unable to handle UnknownHostException [with Retrofit 2, okhttp3, rxandroid 2]

此生再无相见时 提交于 2019-12-25 17:01:14
问题 I've got an Android app which has been in production for some time. I've received a fatal report to Firebase (log is below) but it's not clear to me how to avoid it next time. I've found similar stack trace here java.net.UnknownHostException Unable to resolve host "accounts.google.com": No address associated with hostname while inserting rows in bigquery but they were able resolved it with: <uses-permission android:name="android.permission.INTERNET" /> But that's not my case. For network

Polling using RXJava2 / RXAndroid 2 and Retrofit

大城市里の小女人 提交于 2019-12-25 09:15:32
问题 i would like to implement a Pollingservice which calls a REST Api every nDelay Seconds and notify all subscribers if the data has been changed. Now i have a little problem with my code since it always returns a value to my Consumer, even if the data has not been changed. private Observable<List<HueLight>> pollingLightsObservable = null; public Observable<List<HueLight>> getPollingLightsObservable() { if (pollingLightsObservable == null) { pollingLightsObservable = Observable.fromCallable( ()