Sharing DefaultHttpClient in Android. To synchronize or not to synchronize?
In an Android app, I am using one static instance of org.apache.http.impl.client.DefaultHttpClient and sharing that instance in all activities and services 开发者_开发技巧of the app, so that this client may be used to log in once to a remote server and all subsequent user requests to that remote server will remain authenticated.
Every activity or Service that makes a GET or POST to this remote server calls the same method : MyUtilityClass.gettHttpClient()
Do I need to worry about synchronization of this httpclient? If so, what is the best way to handle this?
Use ThreadSafeConnectionManager, but be sure to call httpResponse.getEntity().consumeContent() after processing the response in order to ensure that connections are released back to the pool.
Use a ThreadSafeConnectionManager
, then you do not need to synchronize.
You can use AndroidHttpClient1. It is properly configured and already uses ThreadSafeClientConnManager
精彩评论