按请求okhttp newbuilder

发布于 2025-01-31 06:44:24 字数 1161 浏览 3 评论 0原文

尝试以正确的方式实现OKHTTP。我了解必须共享OkhttpClient(Singleton),但是我不清楚.newBuilder();

示例代码:

// Instantiated once
private static OkHttpClient client = new OkHttpClient.Builder()
        .readTimeout(readTime, TimeUnit.MILLISECONDS)
        .connectionPool(new ConnectionPool(200, connectTimeout, TimeUnit.MILLISECONDS));
        .build(); 

public static String makeRestCall(String url, String data, Interceptor customInterceptor) {
    // Questions on the line below
    OkHttpClient newClient = client.newBuilder()
        .addInterceptor(customInterceptor)
        .build();

    ....
    try (Response response = newClient.newCall(httpRequest).execute()) {
            final ResponseBody body = response.body();
            return body.string();
    }
    return "NO_DATA";
}
 

有一些问题

  1. 我在.newbuilder()

    时 > newclient ,原始客户端是否也通过参考更新?

  2. 类调用MakerestCall确定他们需要的自定义感应器。可以打电话.newbuilder()为每个请求吗?

我一直在搜索文档并播放实施,但对上述情况并没有清楚。

任何援助/指针都将不胜感激。

Trying to implement okhttp the correct way. I understand the OkHttpClient must be shared (Singleton), however I am not clearly understanding .newBuilder();

Sample Code:

// Instantiated once
private static OkHttpClient client = new OkHttpClient.Builder()
        .readTimeout(readTime, TimeUnit.MILLISECONDS)
        .connectionPool(new ConnectionPool(200, connectTimeout, TimeUnit.MILLISECONDS));
        .build(); 

public static String makeRestCall(String url, String data, Interceptor customInterceptor) {
    // Questions on the line below
    OkHttpClient newClient = client.newBuilder()
        .addInterceptor(customInterceptor)
        .build();

    ....
    try (Response response = newClient.newCall(httpRequest).execute()) {
            final ResponseBody body = response.body();
            return body.string();
    }
    return "NO_DATA";
}
 

I have a few questions around .newBuilder()

  1. When we add a new interceptor to newClient, does the original client also get updated by reference?

  2. Classes calling makeRestCall decide on what customInteceptor they need. Is it ok to call .newBuilder() for every request?

I have been searching the documentation and playing with the implementation but haven't had clarity on the above.

Any assistance/pointers are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晌融 2025-02-07 06:44:24
  1. 当我们将新的拦截器添加到新client时,原始客户端也会通过参考更新吗?

不,原件没有变化。它的配置是不可变的。

  1. 呼叫MakerestCall的课程决定他们需要哪种自定义感受器。每个请求都可以致电.newbuilder()吗?

绝对地。该操作很便宜,因为它仅复制配置。没有复制资源密集型的内容,例如连接池和高速缓存。

  1. When we add a new interceptor to newClient, does the original client also get updated by reference?

No, the original is unchanged. Its configuration is immutable.

  1. Classes calling makeRestCall decide on what customInteceptor they need. Is it ok to call .newBuilder() for every request?

Absolutely. That operation is cheap because it only duplicates the configuration. Resource-intensive stuff like the connection pool and cache are not duplicated.

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