我创建了以下函数来检查连接状态:

private void checkConnectionStatus() {
    HttpClient httpClient = new DefaultHttpClient();

    try {
      String url = "http://xxx.xxx.xxx.xxx:8000/GaitLink/"
                   + strSessionString + "/ConnectionStatus";
      Log.d("phobos", "performing get " + url);
      HttpGet method = new HttpGet(new URI(url));
      HttpResponse response = httpClient.execute(method);

      if (response != null) {
        String result = getResponse(response.getEntity());
        ...

当我关闭服务器测试执行等待很长时间在行

HttpResponse response = httpClient.execute(method);

有人知道如何设置超时以避免等待太长时间吗?

谢谢!


当前回答

一种选择是使用OkHttp客户端,来自Square。

添加库依赖项

在构建中。Gradle,包括这一行:

compile 'com.squareup.okhttp:okhttp:x.x.x'

其中x.x.x为所需的库版本号。

设置客户端

例如,如果你想设置一个60秒的超时,这样做:

final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);

ps:如果你的minSdkVersion大于8,你可以使用TimeUnit.MINUTES。所以,你可以简单地使用:

okHttpClient.setReadTimeout(1, TimeUnit.MINUTES);
okHttpClient.setConnectTimeout(1, TimeUnit.MINUTES);

有关单位的详细信息,请参见TimeUnit。

其他回答

你可以通过HttpClient -android-4.3.5创建HttpClient实例,它可以很好地工作。

 SSLContext sslContext = SSLContexts.createSystemDefault();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslContext,
                SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);
                RequestConfig.Builder requestConfigBuilder = RequestConfig.custom().setCircularRedirectsAllowed(false).setConnectionRequestTimeout(30*1000).setConnectTimeout(30 * 1000).setMaxRedirects(10).setSocketTimeout(60 * 1000);
        CloseableHttpClient hc = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(requestConfigBuilder.build()).build();
HttpParams httpParameters = new BasicHttpParams();
            HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(httpParameters,
                    HTTP.DEFAULT_CONTENT_CHARSET);
            HttpProtocolParams.setUseExpectContinue(httpParameters, true);

            // Set the timeout in milliseconds until a connection is
            // established.
            // The default value is zero, that means the timeout is not used.
            int timeoutConnection = 35 * 1000;
            HttpConnectionParams.setConnectionTimeout(httpParameters,
                    timeoutConnection);
            // Set the default socket timeout (SO_TIMEOUT)
            // in milliseconds which is the timeout for waiting for data.
            int timeoutSocket = 30 * 1000;
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

如果你正在使用默认的http客户端,下面是如何使用默认的http参数:

HttpClient client = new DefaultHttpClient();
HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params, 3000);
HttpConnectionParams.setSoTimeout(params, 3000);

原创版权归http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/所有

在客户端进行设置。

AndroidHttpClient client = AndroidHttpClient.newInstance("Awesome User Agent V/1.0");
HttpConnectionParams.setConnectionTimeout(client.getParams(), 3000);
HttpConnectionParams.setSoTimeout(client.getParams(), 5000);

我已经在JellyBean上成功地使用了这一点,但也应该适用于较旧的平台....

HTH

一种选择是使用OkHttp客户端,来自Square。

添加库依赖项

在构建中。Gradle,包括这一行:

compile 'com.squareup.okhttp:okhttp:x.x.x'

其中x.x.x为所需的库版本号。

设置客户端

例如,如果你想设置一个60秒的超时,这样做:

final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);

ps:如果你的minSdkVersion大于8,你可以使用TimeUnit.MINUTES。所以,你可以简单地使用:

okHttpClient.setReadTimeout(1, TimeUnit.MINUTES);
okHttpClient.setConnectTimeout(1, TimeUnit.MINUTES);

有关单位的详细信息,请参见TimeUnit。