使用Android中的另一个请求/响应协议使用Raturofit
我想使用 retrofit 使用IoT的另一个请求/响应协议调整响应(不是http,而是http,而是在我的Android应用程序中非常相似。值得注意的是,我想另一个协议已经具有执行人,而改造将仅以类型安全的方式调整请求结果。
我看到Retrofit 构建器
用于通过客户端传递的功能与Okhttp紧密耦合:
public Builder client(OkHttpClient client){...}
我已经开始研究Raterofit的 calladapter.factory
,但我不知道他们是否可以从OKHTTP客户端或是否可以独立工作可以绕过这个客户。
有没有一种方法可以将改造与另一个请求/响应协议?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法用
call
call interfaces。首先,我实现了一个自定义
call.factory
。newCall(请求请求)
在call.factory
中是输入点。okhttp3.request
传递到其中,然后返回呼叫
。这是一个高级示例:下一步是创建一个自定义调用。为此,我使用 > 接口。大多数方法都很简单,但是要考虑的是方法
覆盖趣味启动(reverseCallback:pallback)
and fun execute():响应之间的区别,该方法分别对应于非阻滞和阻止执行。最后,可以将自定义
call.factory
通过以下代码进行翻新: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:Raterofit只是Okhttp周围的一个不错的包装器,但是您可以始终直接使用okhttp
Retrofit is just a nice wrapper around okHttp, but you can always use okhttp directly for your purpose