使用Android中的另一个请求/响应协议使用Raturofit

发布于 2025-02-06 18:14:41 字数 665 浏览 2 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2025-02-13 18:14:41

我设法用 call call interfaces。

首先,我实现了一个自定义call.factorynewCall(请求请求)call.factory中是输入点。 okhttp3.request传递到其中,然后返回呼叫。这是一个高级示例:

override fun newCall(request: Request): Call = CustomCall(request, ...)

下一步是创建一个自定义调用。为此,我使用 > 接口。大多数方法都很简单,但是要考虑的是方法覆盖趣味启动(reverseCallback:pallback) and fun execute():响应之间的区别,该方法分别对应于非阻滞和阻止执行。

最后,可以将自定义call.factory通过以下代码进行翻新:

Retrofit.Builder()
        .baseUrl(baseUrl)
        .callFactory(customFactory)
        .build()
        .create(CustomApi::class.java)

I managed to solve this problem rather with OkHttp's Call.Factory and Call interfaces.

First of all, I implemented a custom Call.Factory. The newCall(Request request) method in the Call.Factory is the entry point. An okhttp3.Requestis passed into it and it returns a Call. Here's a high-level example:

override fun newCall(request: Request): Call = CustomCall(request, ...)

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 methods override fun enqueue(responseCallback: Callback) and fun 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:

Retrofit.Builder()
        .baseUrl(baseUrl)
        .callFactory(customFactory)
        .build()
        .create(CustomApi::class.java)
神妖 2025-02-13 18:14:41

Raterofit只是Okhttp周围的一个不错的包装器,但是您可以始终直接使用okhttp

Retrofit is just a nice wrapper around okHttp, but you can always use okhttp directly for your purpose

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文