Android 4.0 org.apache.http.conn.ConnectTimeoutException: Connect to ... timed out
Asked Answered
C

2

16

I am facing a weird problem since I test my applications on ICS.

Using the following code on Android 2.X works well (sometimes timeouts happen but very few times) :

    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParameters, SOCKET_TIMEOUT);

    final DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

    // Create a new HttpClient and Post Header
    HttpPost httpPost = new HttpPost(url);

    HttpResponse response = null;
    try {
        if (keys != null) {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
        }

        // Cookies
        // Create a local instance of cookie store
        if (checkCookieValues()) {
            BasicClientCookie cookieSession = new BasicClientCookie(mCookieName, mCookieValue);
            cookieSession.setDomain(mCookieDomain);
            httpClient.getCookieStore().clear();
            httpClient.getCookieStore().addCookie(cookieSession);
        }

        // Execute HTTP Post Request
        response = httpClient.execute(httpPost);

        httpClient.getConnectionManager().shutdown();

    } catch (UnknownHostException e) {
        Log.e(TAG, "Error when calling postData", e);
    } catch (SocketTimeoutException e) {
        Log.e(TAG, "Error when calling postData", e);
    } catch (ClientProtocolException e) {
        Log.e(TAG, "Error when calling postData", e);
    } catch (SocketException e) {
        Log.e(TAG, "Error when calling postData", e);
    } catch (IOException e) {
        Log.e(TAG, "Error when calling postData", e);
    }

    return response;

On ICS, as soon as I receive a time out exception, all next calls will return a timeout exception.

    Timeout exception received :
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980): org.apache.http.conn.ConnectTimeoutException: Connect to /78.109.91.193:80 timed out
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at com.kreactive.planningtv.service.PlanningTVService.postData(PlanningTVService.java:1554)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at com.kreactive.planningtv.service.PlanningTVService.fbConnect(PlanningTVService.java:1897)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at com.kreactive.planningtv.service.PlanningTVService.onHandleIntent(PlanningTVService.java:569)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at android.os.Handler.dispatchMessage(Handler.java:99)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at android.os.Looper.loop(Looper.java:137)
    02-06 19:06:05.425: E/PLTV:PlanningTVService(2980):     at android.os.HandlerThread.run(HandlerThread.java:60)

Does anyone faced the problem ? Is there a way to avoid this problem ? I checked and didn't find a similar question (for ICS especially).

Thanks for your responses!

Convex answered 6/2, 2012 at 18:27 Comment(3)
can you get any solution for thisFrantz
Did you run this code in a unique thread?Archaeornis
I have similar code on a background thread and notice the same issue whenever changing my target to 4.X. ConnectTimeoutExceptions are thrown randomly, but often. This was not a problem when targetting 2.XTwaddle
P
-2

Within an Android application you should avoid performing long running operations on the user interface thread. This includes file and network access.

StrictMode allows to setup policies in your application to avoid doing incorrect things. As of Android 3.0 (Honeycomb) StrictMode is configured to crash with a NetworkOnMainThreadException exception, if network is accessed in the user interface thread.

While you should do network access in a background thread.

If you are targeting Android 3.0 or higher, you can turn this check off via the following code at the beginning of your onCreate() method of your activity.

StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 

It is not advised to turn this off.

Platypus answered 13/10, 2014 at 4:56 Comment(1)
who soever gave down vote, can you please mention reason here.Platypus
P
-3

The Apache http stack is deprecated and broken in ICS. Chromium is now the standard. Ensure that v8 is your devices default java script engine.

Phone answered 19/2, 2012 at 16:15 Comment(1)
I don't think this is related. Check the like above to have a possible answer we are going to test soon. Anyway, thank you for your answer.Convex

© 2022 - 2024 — McMap. All rights reserved.