Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How can i set a timeout for SSL sockets for a blocking read/write using the ThreadSafeClientConnectionMana­ger?

Tags:

java

android

How can i set a timeout for SSL sockets for a blocking read/write using the ThreadSafeClientConnectionMana­get? I've found that losing network connectivity while reading or writing a SSL socket results in a 15 minute timeout on Android OS 2.2 and 2.3 devices.

I set the following timeouts on my HttpClient:

    mParams = new BasicHttpParams();
    HttpProtocolParams.setVersion(mParams, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(mParams, "UTF-8");
    HttpProtocolParams.setUserAgent(mParams, USER_AGENT);

    HttpConnectionParams.setConnectionTimeout(mParams, TIME_OUT);
    HttpConnectionParams.setSoTimeout(mParams, TIME_OUT);

    ConnManagerParams.setTimeout(mParams, TIME_OUT);

    final SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme(HTTP_SCHEME, PlainSocketFactory.getSocketFactory(), 80));
    registry.register(new Scheme(HTTPS_SCHEME, SSLSocketFactory.getSocketFactory(), 443));

    final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(mParams, registry);

    mClient = new DefaultHttpClient(manager, mParams);

I then use the client to perform a http put request with a file entity. If I turn on airplane mode in mid upload, wait 15-30 seconds and then turn off airplane mode the socket will be stuck in either a read or write and won't timeout for 15 mins.

like image 891
Todd Avatar asked Feb 08 '12 23:02

Todd


1 Answers

Not to pivot away from your very legitimate question, but the blog post Android's HTTP Clients recommends using HttpURLConnection for new development. Have you considered switching APIs?

like image 100
Sparky Avatar answered Nov 12 '22 05:11

Sparky