Pec*_*277 1 java connection jsoup
我正在使用JSoup连接到网站.我有时会发现 JSoup会有连接超时,当发生这种情况时我想 JSoup重试连接,当它第3次失败时它会在数组列表中添加一个字符串.
我的代码目前是:
try {
Document doc = Jsoup.connect(sitemapPath)
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21")
.timeout(10000)
.get();
Elements element = doc.select("loc");
return element;
} catch (IOException e) {
return null;
}
Run Code Online (Sandbox Code Playgroud)
我想用while循环做一些事情,但我必须返回元素,所以我不确定如何做到这一点.
ArrayList<String> l = new ArrayList();
Document doc = null;
int i = 0;
boolean success = false;
while( i < 3){
try {
doc = Jsoup.connect(sitemapPath).get();
success = true;
break;
} catch (SocketTimeoutException ex){
l.add("text...");
}
catch (IOException e) {
}
i++;
}
if(success){
// Selector code ...
Elements element = doc.select("loc");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4849 次 |
| 最近记录: |