Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8406061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:59:48+00:00 2026-06-09T22:59:48+00:00

In my app more than one IntentService connect/communicate with a Tomcat server at random

  • 0

In my app more than one IntentService connect/communicate with a Tomcat server at random intervals depending on user interaction with the application.

I am using the following Singleton for Http

public class CustomHttpClient { 
    private static HttpClient customHttpClient; 
    /** A private Constructor prevents instantiation */ 
    private CustomHttpClient() { 
    } 

    public static synchronized HttpClient getHttpClient() { 
        if (customHttpClient == null) { 
            HttpParams params = new BasicHttpParams(); 
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
            HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
            HttpProtocolParams.setUseExpectContinue(params, true); 
            HttpProtocolParams.setUserAgent(params,"Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83)" +
                    " AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"); 
            ConnManagerParams.setTimeout(params, 1000); 
            HttpConnectionParams.setConnectionTimeout(params, 5000); 
            HttpConnectionParams.setSoTimeout(params, 10000); 
            SchemeRegistry schReg = new SchemeRegistry(); 
            schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
            schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 
            ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params,schReg); 
            customHttpClient = new DefaultHttpClient(conMgr, params); 
        } 
        return customHttpClient; 
    } 
    public Object clone() throws CloneNotSupportedException { 
        throw new CloneNotSupportedException(); 
    } 
}

The class works fine except it gives connectTimeOut whenever two IntentService try to Http at the same time or when one connection is in progress and another connection is established.

One obvious improvement in the code is to replace deprecated ThreadSafeClientConnManager with PoolingClientConnectionManager.

Should I synchronize here -> public static synchronized HttpClient getHttpClient()

What error in the Singleton is causing the connectTimeOut?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:59:50+00:00Added an answer on June 9, 2026 at 10:59 pm

    Synchronize the PoolingClientConnectionManager or not?

    Yes and no. If you want getHttpClient() to be a singleton (IMO a good idea) you have to make it thread safe. That requires the synchronized. Without synchronized two thread can enter the if block at the same time (because customHttpClient == null is true for both threads) and create two instances.

    But there are better (as in faster) ways without synchronized to create thread-safe singletons. I like the Singleton Holder approach for example.

    But regardless of the singleton you use here you should not get connection timeouts. It should also work if you use new HttpClient instances for each thread.


    If a connection timeout happens then one of your threads did not manage to connect to the server within the time limit you have set with HttpConnectionParams.setConnectionTimeout(params, 5000); = 5 seconds.

    That can have several reasons. For example if you are on a slow & instable connection it may take longer because it can happen that your connection is dead for several seconds. Or if your server can’t handle more connections either due to the configuration (e.g. a connection limit per IP) or because the hardware can’t handle more you can see this problem too. Basically anything that prevents the HttpClient to send and receive packets to / from the server can trigger that problem. Means your problem is either on the device or in the network or on the server.

    I don’t know your network setup but you could try to increase the timeout and see if that has any effect. The AndroidHttpClient for example sets those timeouts to 60 seconds. That is much more than required if you are on WiFi and have a stable connection but is good when the connection is really weak.

    You could also check if the other settings that AndroidHttpClient applies help.

    Setup of AndroidHttpClient below

    // Default connection and socket timeout of 60 seconds.  Tweak to taste.
    private static final int SOCKET_OPERATION_TIMEOUT = 60 * 1000;
    
    // ---------------------------------------------------------------------- //
    HttpParams params = new BasicHttpParams();
    
    // Turn off stale checking.  Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    
    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    
    // Don't handle redirects -- return them to the caller.  Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    HttpClientParams.setRedirecting(params, false);
    
    // Use a session cache for SSL sockets
    SSLSessionCache sessionCache = context == null ? null : new SSLSessionCache(context);
    
    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http",
            PlainSocketFactory.getSocketFactory(), 80));
    schemeRegistry.register(new Scheme("https",
            SSLCertificateSocketFactory.getHttpSocketFactory(
            SOCKET_OPERATION_TIMEOUT, sessionCache), 443));
    
    ClientConnectionManager manager =
            new ThreadSafeClientConnManager(params, schemeRegistry);
    
    return new DefaultHttpClient(manager, params);
    

    To determine where your problem lies you could have a look into your server connection logs and check if the device tries to initiate a connection and if so if there are reasons why the connection is not established. If you don’t see any connection attempts when the timeout happens it is more likely a device or network problem.

    If you have access to the network the device is connected to (=WiFi) you could check the network traffic with e.g. Wireshark for reasons the connection attempt times out. You can see there if the device sends a connection request or not.

    And last but not least it can be caused by some of your code. If you find no other solution try to implement it with HttpURLConnection and see if that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a JSP/Servlet Web App that consist of more than one servlet (and some
In eclipse is there a way to run more than one Java app in
Does more than one instance of a controller get created per App-Domain? If so
I want to publish my application for more than one Version of Android. Thats
if you have an android app that has more than one Activity, and Activity
I can't create more than one table on a Chrome App dadabase Here is
My application needs to talk to more than one secure servers. I am using
More than one year ago I submitted an app. Now I want to update
The users of my web application may have more than one browser window open
How to associate more than one file extension types to my iPhone App already

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.