开发者

Android: One vs many instances of HttpClient per application

Until recently, our app shared a single Apache HttpClient instance using the ThreadSafeClientConnManager across the whole application. The http client instance was held by a singleton class.

Since I dislike the singleton pattern 开发者_高级运维for its numerous problems, I refactored our API accessor to be a per-thread object, but now for every thread (which mostly means per Activity/Service in our case) a new HttpClient instance is created.

It's not that I have problems with this new approach, but I've read that the Apache folks suggest to only have one instance per app for performance reasons.

Visually, what we did before was this:

HttpClient (thread safe)
          |
          |
         /\
        /  \
Activity1...ActivityN

Now, we do this:

Activity1 ... ActivityN
    |             |
    |             |
HttpClient1   HttpClientN

How do you guys do this in your applications? If you share one single HttpClient across your app and potentially many concurrent threads, how do you handle access to it?


In a nutshell:

Create an instance of org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager and use it when constructing a DefaultHttpClient.

Link to sample source: HttpClient multithreaded access

Edit: Sorry, didn't see your edit before posting. There's nothing inherently wrong with "Singleton" in this case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜