package jsoup;
import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import common.Utils;
public class ConnectionManager {
private static ConnectionManager instance = null;
public static ConnectionManager getInstance(){
if (instance == null) {
instance = new ConnectionManager();
}
return instance;
}
public Connection getConnection(String url){
return Jsoup.connect(url).timeout(60000);
}
public Document getDocument(String url) throws Exception{
Connection conn = getConnection(url);
if (conn == null) {
throw new Exception("url connect error..."+url);
}
Document doc = null;
int i = 1;
while (doc == null) {
try {
doc = conn.get();
} catch (IOException e) {
i++;
if (i>20) {
Utils.print("***********("+e.getMessage()+")Try to read ("+i+") times.");
return null;
}
conn = getConnection(url);
}
}
return doc;
}
}