我希望我的设备在 5 秒后放弃 http 连接。但是我的代码不起作用......关闭网络时我从未收到任何超时消息。就像设备仍然尝试连接一样,尽管超时......
有想法吗?我是否试图捕捉正确的异常?
谢谢。
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpResponse response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() < 400)
{
... //data processing
}
else
{
errorMsgId = R.string.http_site_error;
}
}
catch (ConnectTimeoutException e)
{
Toast.makeText(this, "Network timeout reached!", Toast.LENGTH_SHORT).show();
Log.e("+++++++++++++++++ ","Network timeout reached!");
}