On Wed, Jun 24, 2009 at 03:45:51PM +0200, Imre Fazekas wrote:
> Dear All,
>
>
> i'm just wondering when the DefaultHttpClient class should be
> instantiated.
> I have a class providing basic HTTP services like sending JSON message,
> sending a get and parsing the response, etc. So this class has static
> methods.
> I defined these members with these initializations:
> private static HttpParams defaultParameters;
> private static SchemeRegistry supportedSchemes;
> private static ClientConnectionManager clcm;
> static{
> setup();
> clcm = createManager();
> }
>
> private static final void setup() {
> supportedSchemes = new SchemeRegistry();
> SocketFactory sf = PlainSocketFactory.getSocketFactory();
> supportedSchemes.register(new Scheme("http", sf, 80));
>
> HttpParams params = new BasicHttpParams( );
> HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
> HttpProtocolParams.setUseExpectContinue(params, false);
>
> defaultParameters = params;
>
> context = new BasicHttpContext( );
> }
>
> private static final ClientConnectionManager createManager() {
> return new ThreadSafeClientConnManager( defaultParameters,
> supportedSchemes );
> }
>
>
> May i define the HttpClient as a static field or i should define always a
> new instance when a service method is called? Like this:
You may and you should. In short one should have:
1 HttpClient per service / application
1 HttpContext per thread / user
1 HttpRequest per request execution
Oleg
> public static Response get(String uri) {
> DefaultHttpClient httpClient = getHttpClient();
> HttpGet get = new HttpGet( serverURI + uri );
> ...
> }
>
>
>
> Thank you in advance!
>
> Regards,
>
> Imre
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]