带有选项卡主机的 HttpClient
我有一个带有 TabHost 的应用程序,其中有几个选项卡,在每个选项卡中我都显示一些使用 HttpClient 从服务器获取它的网页。在每个选项卡上打开并实例化一个新的 HttpClient 是一个好习惯吗?
I have an application with TabHost with few tabs and in each tab I am displaying some web-page getting it from the server using HttpClient. Is it a good practice to open and instantiate a new HttpClient on every tab?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您在每个选项卡中显示不同的网页,我会为每个选项卡使用一个 HttpClient。但这实际上取决于您的具体要求。例如,如果您不希望在未显示时更新选项卡,则可以重用 HttpClient 实例。当然,这会让您承担在选项卡切换之间管理 HttpClient 状态的责任。
If you display a different web page in each tab, I'd go with one HttpClient per tab. But it really depends on what your exact requirement. For example, if you do not wish to update a tab while it is not being shown, you could reuse your HttpClient instance. That would, of course, place the onus of managing the state of the HttpClient between tab switches on you.
一般规则:
重用代码和采用模块化方法始终是一个很好的做法。您始终可以使用静态方法创建一个单独的类来满足您的要求,而不用重做它。
在您的使用中,此设置如何要求您通过在每个选项卡中实例化新的 HTTPClient 来粘贴一些代码来说明您的含义。
General rule:
It is always a good practice to re-use code and take modular approach. You can always create a separate class with static methods to satisfy your requirements instead of redoing it.
How does this set in your use would require you to paste some code as to what you mean by instantiating a new HTTPClient in each tab.