0

我正在尝试通过网络在线程上加载信息。当没有互联网时,它会冻结很长时间,然后才会引发异常或只是冻结。

有没有办法为 // FREEZES HERE 设置超时,或者在没有互联网时需要很长时间才能通过异常?他们是一种设置超时的方法response = httpclient.execute(httppost);吗?

HttpClient httpclient = new DefaultHttpClient();
HttpPost  httppost = new HttpPost("http://besttechsolutions.biz/projects/bookclub/getevents.php");
// FREEZES HERE or takes a long time to through exception when there is no internet
response = httpclient.execute(httppost);
responseBody = EntityUtils.toString(response.getEntity());
4

1 回答 1

0

试试下面的。

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);

if(null == httpClient) 
httpClient = new DefaultHttpClient(httpParameters);

上面的代码一般为httpClient设置了一个默认的超时时间。要检查互联网访问,请参阅 Nomand 发布的链接。

于 2013-03-12T17:30:43.050 回答