本文翻譯自:How to set HttpResponse timeout for Android in Java
I have created the following function for checking the connection status: 我創建了以下用於檢查連接狀態的函數:
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());
...
When I shut down the server for testing the execution waits a long time at line 當我關閉服務器進行測試時,執行會等待很長時間
HttpResponse response = httpClient.execute(method);
Does anyone know how to set the timeout in order to avoid waiting too long? 有誰知道如何設置超時以避免等待太久?
Thanks! 謝謝!
#1樓
參考:https://stackoom.com/question/2uXV/如何在Java中爲Android設置HttpResponse超時
#2樓
To set settings on the client: 要在客戶端上設置設置:
AndroidHttpClient client = AndroidHttpClient.newInstance("Awesome User Agent V/1.0");
HttpConnectionParams.setConnectionTimeout(client.getParams(), 3000);
HttpConnectionParams.setSoTimeout(client.getParams(), 5000);
I've used this successfully on JellyBean, but should also work for older platforms .... 我已經在JellyBean上成功使用了它,但也適用於舊平臺....
HTH HTH
#3樓
In my example, two timeouts are set. 在我的示例中,設置了兩個超時。 The connection timeout throws java.net.SocketTimeoutException: Socket is not connected
and the socket timeout java.net.SocketTimeoutException: The operation timed out
. 連接超時拋出java.net.SocketTimeoutException: Socket is not connected
且套接字超時java.net.SocketTimeoutException: The operation timed out
。
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);
If you want to set the Parameters of any existing HTTPClient (eg DefaultHttpClient or AndroidHttpClient) you can use the function setParams() . 如果要設置任何現有HTTPClient的參數(例如DefaultHttpClient或AndroidHttpClient),可以使用函數setParams() 。
httpClient.setParams(httpParameters);
#4樓
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);
#5樓
If you are using the HttpURLConnection
, call setConnectTimeout()
as described here : 如果您正在使用HttpURLConnection
,請按照此處所述調用setConnectTimeout()
:
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(CONNECT_TIMEOUT);
#6樓
you can creat HttpClient instance by the way with Httpclient-android-4.3.5,it can work well. 你可以通過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();