Android 4.0 org.apache.http.conn.ConnectTimeoutException:连接到...超时

时间:2012-02-06 18:27:46

标签: android timeout android-4.0-ice-cream-sandwich

因为我在ICS上测试我的应用程序,所以我遇到了一个奇怪的问题。

在Android 2.X上使用以下代码效果很好(有时会发生超时但很少次):

    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;

在ICS上,一旦收到超时异常,所有下一次调用都将返回超时异常。

    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)

有人遇到过这个问题吗?有没有办法避免这个问题? 我检查过并没有找到类似的问题(尤其是ICS)。

感谢您的回复!

2 个答案:

答案 0 :(得分:-1)

在Android应用程序中,您应该避免在用户界面线程上执行长时间运行的操作。这包括文件和网络访问。

StrictMode 允许在应用程序中设置策略,以避免执行不正确的操作。从 Android 3.0(Honeycomb)开始,如果在用户界面线程中访问网络,则StrictMode配置为因NetworkOnMainThreadException异常而崩溃。

虽然您应该在后台线程中进行网络访问。

如果您的目标是Android 3.0或更高版本,则可以在活动的onCreate()方法开头通过以下代码关闭此检查。

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

建议不要关闭它。

答案 1 :(得分:-3)

在API中不推荐使用Apache http堆栈。铬现在是标准。确保v8是您的设备默认java脚本引擎。