java - Java如何设置安卓HttpResponse的超时时间

  显示原文与译文双语对照的内容

我已经创建了以下检查连接状态的函数:


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);

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

谢谢!

时间:

在我的示例中,设置了两个超时。 连接超时"java.net.SocketTimeoutException: 套接字未连接"套接字超时"java.net.SocketTimeoutException: 操作超时"。


HttpGet httpGet = new HttpGet(url);
HttpParams httpParameters = new BasicHttpParams();
//Set the timeout in milliseconds until a connection is established.
//The default value is zero, that means the timeout is not used. 
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
//Set the default socket timeout (SO_TIMEOUT) 
//in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(httpGet);

如果你想设置任何现有HTTPClient的参数( 例如。 DefaultHttpClient或者 AndroidHttpClient ) 你可以使用函数 setParams()


httpClient.setParams(httpParameters);

要在客户端设置设置:


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

我已经在小糖果上成功使用了这个,但也应该适用于较老的平台。

HTH

如果你使用的是 http客户端库的,那么你可以执行以下操作:


 HttpClient client = new HttpClient();
 client.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(5000));
 client.getParams().setParameter(HttpClientParams.SO_TIMEOUT, new Integer(5000));
 GetMethod method = new GetMethod("http://www.yoururl.com");
 method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(5000));
 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
 int statuscode = client.executeMethod(method);


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);

你可以通过httpclient-android-4的方式创建HttpClient实例。3 。5,它可以很好地工作。


 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();

...