我有以下代码:
try{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader r = sp.getXMLReader();
//This handles the xml and populates the entries array
XMLHandler handler = new XMLHandler();
// register event handlers
r.setContentHandler(handler);
String url = "http://news.library.ryerson.ca/api/isbnsearch.php?isbn="+ISBN;
r.parse(url);
return handler.getEntries();
}
此代码在大多数情况下都可以正常工作,但在某些情况下,用户可以输入具有 100 多个相关 ISBN 的流行书籍的 ISBN(例如《哈利·波特》)。发生这种情况时,XML 提要不会中断,但加载时间会更长(极端情况下可能长达 30 秒以上)。当页面加载时,它永远不会断开连接,只是需要时间加载。
有没有办法增加函数的超时时间?
谢谢
最佳答案
//opens the URL as a stream, so it does not timeout prematurely
String u = new String("http://foobar/isbnsearch.php?isbn="+ISBN);
URL url = new URL(u);
InputStream stream = url.openStream();
r.parse(new InputSource(stream));
stream.close();
我自己通过添加这个解决了这个问题。
关于java - SAXParserFactory URL 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3346949/