I want to use Retrofit to adapt responses using another request/response protocol for IoT (not HTTP, but very similar in terms of architecture) in my Android application. Notably, I suppose the other protocol already has an executor, and Retrofit would come in just to adapt the request results in a type-safe fashion.
I saw that the Retrofit Builder
function used for passing in the client is strongly coupled to OkHttp:
public Builder client(OkHttpClient client){...}
I have started looking into Retrofit's CallAdapter
and CallAdapter.Factory
but I do not know if they can work independently from an OkHttp client or if they can bypass this client.
Is there a way to use Retrofit with another request/response protocol?
I managed to solve this problem rather with OkHttp's
Call.Factory
andCall
interfaces.First of all, I implemented a custom
Call.Factory
. ThenewCall(Request request)
method in theCall.Factory
is the entry point. Anokhttp3.Request
is passed into it and it returns aCall
. Here's a high-level example:The next step is to create a custom call. For that purpose, I used the
Call
interface. Most methods are quite straightforward, but something to consider is the difference between the methodsoverride fun enqueue(responseCallback: Callback)
andfun execute(): Response
, which correspond respectively to non-blocking and blocking executions.Finally it is possible to pass the custom
Call.Factory
to Retrofit with the following code: