这些错误对于访问互联网的应用程序来说是很正常的。
但是,您可以尝试通过更改 HTTP 请求的超时值来减少它们的发生,如下所示:
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);