我昨天已经发过这个问题了,但是没有得到解决,今天重新发帖,希望得到完美的解决方案。
在下面这个程序里,我使用它读取网页,但是有时候程序会被卡住在line = reader.readLine()) != null这一行里,总是读不出来,所以程序就停住了。我希望能够有一个时间限制住读取一个网页的时间,超过这个时间就重新读取这个网页。请问我应该如何做?// 根据一个网址提取这个网址的网页内容
public  String getHTMLResource(String htmlFile) throws IOException {//读取URL指定的网页内容
StringBuilder Content =new StringBuilder();
try {
String line = null;
URL url = new URL(htmlFile);// 根据网址创建URL对象
URLConnection conn = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {//读取文件信息
// 记住在这里,文件的末尾多加了一个换行符,这里不清除他了。
Content.append(line+"\n");
}
reader.close();
} catch (Exception e) {}
return Content.toString();
}