android


RxJava/Retrofit - How can I force users to use a specific subclass of Subscriber?


Before using rx.Observable, I used a custom callback with retrofit so I can add some specific logic for handling response/error and not have to do that inside the callback for every request as boilerplate code.
I force users to use the custom callback by putting it in the method signature like this:
#GET("/user_endpoint/")
void getUser(CustomCallback<User> callback);
#GET("/profile_endpoint/")
void getProfile(CustomCallback<Profile> callback);
but now that I'm returning an Observable:
#GET("/user_endpoint/")
Observable<User> getUser();
#GET("/profile_endpoint/")
Observable<Profile> getProfile();
I can't figure out a way to make sure that a custom callback always proxies the error/response.
Also, with retrofit2.0, how can I force the user to use a custom callback with the returned Call object?
CustomCallback for reference:
public abstract class CustomCallback<T> implements Callback<T> {
#Override public final void success(T t, Response response) {
// do some logic
onSuccess(t);
}
#Override public final void failure(RetrofitError error) {
// do something with the error here such as show a Toast
Toast.makeText(Application.getInstance(), error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
onFailure(error);
}
public abstract void onSuccess(T response);
public abstract void onFailure(Throwable error);
}
Stop. You're thinking this the wrong way.
Instead consider this: You have the normal Retrofit interface:
interface Foo {
#GET("/user_endpoint/")
Observable<User> getUser();
}
And then you have your decorator class:
public class FooDecorator implements Foo {
private Foo delegate = ...; // inject or create the Retrofit instance.
#Override
public Observable<User> getUser() {
return delegate.getUser().doOnNext(...).doOnError(...);
}
}
Then you use only the second class everywhere in your code (preferably just let the DI system use that) and you're set.
If you're feeling adventurous, you could even adapt the RxJavaCallAdapterFactory so that it modifies the returned observables without the need of a custom class.

Related Links

Is there way for extracting string resources in Android project in IntelliJ IDE?
Creating a listener onRotate()
Button size and background color change on button press
How do I find Debug Port of Process if not started from eclipse?
Android: Signing for many customers (MonoDroid)
moving an object in a striaght line
how to send and retrieve message through android to PC(How PC and Android device Communicate using USB)
Android :: OnTouchListener && OnClickListener combination issue
Android Multiple Screen Support Problem
How to upgrade data content on installing new version package?
How can I acquire access to my android app's database and context from JUnit test?
Android - Shows underscore instead of space - HTC EVO 4G
Android Screen Burn - Droid X - Live wallpaper
how to get the text of a radiobutton created dynamically?
OptionsMenu appears in double
Android: passing text from an app to another one

Categories

HOME
search
gridview
delphi-7
fuelux
c++11
phpmyadmin
jax-rs
pda
angular-formly
toolbox
knitr
seo
entity-framework-core
alsa
cakephp-3.4
zip
x264
polling
tvos
google-admin-sdk
data-visualization
gluon-mobile
title
barcode
mql5
mel
text-mining
x-cart
enterprise-architect
http-post
google-content-api
jmp
spring-async
imagemap
event-store
stackexchange
angular-translate
tpm
user-defined-functions
mql
nsurl
boost-asio
google-cast
cognos-bi
database-comparison
indy10
modalviewcontroller
zoomify
openalpr
jpad
health-monitoring
typesafe-config
ogc
portforwarding
movilizer
lpsolve
apollostack
placeholder
berkeley-db-je
photos
user-defined-fields
twilio-click-to-call
bbpress
monkey
offline.js
cmp
branch-prediction
rmongo
donations
slackware
simplepie
segment-io
udpclient
android-wake-lock
ikiwiki
phpgrid
directdraw
sciruby
themoviedb-api
extjs-grid
dock
airbrake
alloy-ui
bcache
codeplex
rcaller
draw2d-js
sbcl
http-patch
inputmismatchexception
model-driven-development
ebay-lms
route-provider
dolby-audio-api
android-icons
superscrollorama
appjs
usertype
google-maps-mobile
ikimagebrowserview
cdonts
silent
interprocess
pos-for-.net
strtod
feasibility
fault-tolerance
linfu-dynamicproxy
blind
cardspace

Resources

Encrypt Message