I am new to RxJava so please forgive me if this sounds too newbie :-).
As of now I have an abstract CallbackClass that implements the Retofit Callback. There I catch the Callback's "onResponse" and "onError" methods and handle various error types before finally forwarding to the custom implemented methods. I also use this centralized class to for request/response app logging and other stuff.
For example: for specific error codes from my sever I receive a new Auth token in the response body, refresh the token and then clone.enqueue the call. There are of course several other global behaviors to the responses from my server.
Current solution (Without Rx):
public abstract void onResponse(Call<T> call, Response<T> response, boolean isSuccess);
public abstract void onFailure(Call<T> call, Response<T> response, Throwable t, boolean isTimeout);
@Override
public void onResponse(Call<T> call, Response<T> response) {
if (_isCanceled) return;
if (response != null && !response.isSuccessful()) {
if (response.code() == "SomeCode" && retryCount < RETRY_LIMIT) {
TokenResponseModel newToken = null;
try {
newToken = new Gson().fromJson(new String(response.errorBody().bytes(), "UTF-8"), TokenResponseModel.class);
} catch (Exception e) {
e.printStackTrace();
}
SomeClass.token = newToken.token;
retryCount++;
call.clone().enqueue(this);
return;
}
}
} else {
onResponse(call, response, true);
removeFinishedRequest();
return;
}
onFailure(call, response, null, false);
removeFinishedRequest();
}
@Override
public void onFailure(Call<T> call, Throwable t) {
if (_isCanceled) return;
if (t instanceof UnknownHostException)
if (eventBus != null)
eventBus.post(new NoConnectionErrorEvent());
onFailure(call, null, t, false);
removeFinishedRequest();
}
My question is: Is there any way to have this sort of centralized response handling behavior before finally chaining (or retrying) back to the subscriber methods?
I found these 2 links which both have a nice starting point but not a concrete solution. Any help will be really appreciated.
Two links you provided are a really good starting point, which I used to construct solution to react to accidental
I have overriden
CallAdapter.Factory
to handle errors and react appropriately to them.Import
RetryWithDelayIf
from the solution you foundOverride
CallAdapter.Factory
to handle errors:HttpServerException
is just a custom exception.Use it in
Retrofit.Builder
Extra: If you wish to parse errors that come from API (error that don't invoke
UnknownHostException
,HttpException
orMalformedJsonException
or etc.) you need to overrideFactory
and use custom one during buildingRetrofit
instance. Parse the response and check if it contains errors. If yes, then throw error and error will be handled inside the method above.See how you can do it. Here is api call and pass Request model and response model in this.
This is the retrofit call,I used retrofit2
This is the api call, call it in your activity.
Hope this will help you.
have you consider using the rxjava adapter for retrofit? https://mvnrepository.com/artifact/com.squareup.retrofit2/adapter-rxjava/2.1.0 in your gradle file add
here's a interface for retrofit
and here's my implementation
please note I'm using the gson converter factory to parse my response so I get an pojo (Plain Ole Java Object) returned.