我试图在线程上通过网络加载信息。当没有互联网时,它会在引发异常或冻结之前冻结很长时间。
有没有办法为这里的FREEZES设置超时,或者在没有互联网时需要很长时间才能通过例外?
他们可以设置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());
答案 0 :(得分:0)
请尝试以下操作。
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 30000);
HttpConnectionParams.setSoTimeout(httpParameters, 30000);
if(null == httpClient)
httpClient = new DefaultHttpClient(httpParameters);
以上代码通常会为httpClient设置默认超时。要查看互联网访问权限,请参阅Nomand发布的链接。